This seems very basic but I'm not having any luck finding an answer.
I would like to call a function like this:
const result = myFunction({
'keyA': [ 'arrayA1', 'arrayA2' ],
'keyB': [ 'arrayB1', 'arrayB2' ],
});
I would like the result to be a key/value Record, where the keys are limited to keyA and keyB, and the value are also key/value records, with keys as arrayA1, arrayA2, etc:
result = {
keyA: {
arrayA1: 'example',
arrayA2: 'example',
},
keyB: {
arrayB1: 'example',
arrayB2: 'example',
}
}
I'm struggling to come up with a generic function signature that will take the keys and array items in the parameter and turn them into the nested object keys.
function myFunction<
T extends { [K in keyof T]: string[] },
S extends keyof T,
O extends T[K] // ???
//O extends { InstanceType<T[K]> } // nope
//O extends { [K in keyof T]: { [K2 in keyof T[K]]: K2 } } // very confused
>(structure: T): Record<S, Record<O, string>> { ... }
I can't seem to work out how to extract the nested string array, and convert it into a type specifying only those values as permitted strings. What am I missing?