I am trying to create a simple typescript program,
which constraints an object to match a given interface data type.
For example, I have an interface Config and a schema object
I want abc.value to be restricted as string while xyz.value to be restricted as number:
interface Config {
abc: string
xyz: number
}
const schema: XXX = {
abc: {
value: '',
},
xyz: {
value: 0,
},
}
I tried using infer keyword but cannot get it work properly.
const schema: Record<T extends keyof Config ? infer keyof Config : any, any> = {
abc: {
value: '',
},
xyz: {
value: 0,
},
}
what I doing wrong here? Any help would be appreciated
Thank you.
Please use mapped types:
Playground