Trying to create a type that only allows certain keys from a union using the key in
statement has resulted in all keys being required. How can I define this type so that not all values have to be present?
const widgetOptions = ['option1', 'option2', 'option3'] as const
export type WidgetOption = (typeof widgetOptions)[number]
export type WidgetData = {
[key: string]: {
[key in WidgetOption]?: number
}
}
const widgets: WidgetData = {
'item1': {
option1: 10,
// no error here without providing option2 or option3
},
'item2': {
option1: 20,
option2: 30
}
} satisfies WidgetData