I have an object with multiple arrays of strings (hardcoded). I want to specify that only strings from that object are allowed in another empty array.
In a simple non-nested scenario, I'm achieving this with
typeof someArray[number][]
. So, I hoped to do the same with a nested array like this
typeof someObjectWithArrays[string][number][]
, but it didn't work :/
I think the following example will better illustrate what I mean:
const demo1 = ['test1', 'test2', 'test3'] as const
const demo2 = {
a: ['test4', 'test5', 'test6'],
b: ['test7', 'test8', 'test9'],
} as const
const values = {
demo1: [] as typeof demo1[number][], // allow 'test1' 'test2' 'test3'
demo2: [] as typeof demo2[string][number][], // ????? allow 'test4' 'test5' 'test6' 'test7' 'test8' 'test9'
}
console.log(values.demo1.includes('test1')) // ok
console.log(values.demo1.includes('notexists')) // error, so ok
console.log(values.demo2.includes('test8')) // ????? should be ok
console.log(values.demo2.includes('notexists')) // ????? should be error