I'm trying to get TS generics to map to a new object. In short, I'm trying to convert:
{
key: { handler: () => string },
key2: { hander: () => number },
}
to:
{ key: string, key2: number }
full example:
type programOption = {
validator: () => unknown
}
type programConfig<T extends {[key: string]: programOption} = {}> = {
options: T,
handler: (data: mapProgramConfig<T>) => void,
}
type mapProgramConfig<T extends {[key: string]: programOption}> = {
[K in keyof T]: ReturnType<programOption['validator']>
}
type mapProgramConfigHardcoded<T> = {
fruit: string,
animal: number
}
class Program {
constructor (config: programConfig) {}
}
const foo = new Program({
options: {
'fruit': { validator: () => 'asdf' },
'animal': { validator: () => 42 },
},
handler: ({fruit, animal, thing}) => {
},
});
Exactly what I'm trying to do can be seen if you replace mapProgramConfig with mapProgramConfigHardcoded in the programConfig type, but I can't seem to make it work in the generic case.