Currently, I am attempting to utilize TypeScript generics in order to transform them into a new object structure. Essentially, my goal is to change:
{
key: { handler: () => string },
key2: { hander: () => number },
}
to:
{ key: string, key2: number }
Here's a complete 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}) => {
},
});
If you replace mapProgramConfig
with mapProgramConfigHardcoded
in the programConfig
type, you can see what I'm trying to achieve. However, I am struggling to make it work in the generic scenario.