I'm quite new to TypeScript and I need to declare a type like:
type U = { [key: string]: number }; // any string for the keys
type T = { [key: string]: U }; // any string for the keys too
Example of use:
const test: T = {
'aaa': {
'xxx': 777,
'yyy': 888,
'zzz': 999 // error expected
},
'bbb': {
'xxx': 444,
'yyy': 555,
'ooo': 666 // error expected
}
};
Is there a way, a better and probably more complex signature, to ensure that all U properties in T will have the same keys?
oooa typo and you want to see an error there? Is it intended but you want to see an error thatzzzis missing? Are the particular keysxxx,yyyandzzzimportant or do you want the type to enforce that whatever the keys are of the nested properties they are the same across each property? Etc etc etc. I'm hoping you can write out some more examples of what things should and should not be allowed. Depending on how complex it is, your requirements might not be expressible as a specific type and you will instead need a generic constraint.