Given a type and variable:
type Form = {
controls: {
[key: string]: any;
}
}
const form: Form = {
controls: {
firstName: {},
lastName: {},
},
};
I want to create a type which is an object containing all keys of form.controls with a value type of string ie:
const formControlsWithStrings = {
firstName: 'foo',
lastName: 'bar',
};
I have tried this:
type FormControlsWithStrings<T extends Form> =
{ [key in keyof T['controls']] : string }
however if I use it to type formControlsWithStrings above, it enforces the string value type, but not that the keys are equivalent -it does not require all keys to be present, and I can add new keys.
Why doesn't this work, and how can I get it to work?
Thanks
Stackblitz: https://stackblitz.com/edit/typescript-sxvspk?file=index.ts