i have two literal union types of strings and want them to be the possible keys and values of an object.
export type AlarmKeyword = 'R1' | 'R2';
export type ResourceTypes = 'nktw' | 'rtw' | 'nef' | 'rth' | 'hlf' | 'dlk';
The new type looks like this:
export type AlarmKeywordObject = {
[P in AlarmKeyword]: { [T in ResourceTypes]: number };
};
But TS now complains about this code:
{
R1: {
rtw: 1,
},
}
Type '{ rtw: number; }' is missing the following properties from type '{ nktw: number; rtw: number; nef: number; rth: number; hlf: number; dlk: number; }': nktw, nef, rth, hlf, dlk
How can i ensure, that the keys are optional and not all of them are required?
Thank you!
CodePudding user response:
You can use question mark ?
to mark that keys are optional:
type AlarmKeywordObject = {
[P in AlarmKeyword]?: { [T in ResourceTypes]?: number }
};