I generate a Type Option
from object Options
using typeof
and keyof
.
I define another type - Dropdown
which have computed property and use the Option
using in
.
I get an error:
Property 'options2' is missing in type '{ [Options.option1]: string; }'.
How should I implement it right?
const Options = {
option1: 'option1' as 'options1',
option2: 'option1' as 'options2',
option3: 'option1' as 'options3',
}
type Option = typeof Options[keyof typeof Options];
type Dropdown = { [key in Option]: string };
const obj: Dropdown = {
[Options.option1]: 'test'
}
Thanks to @TitianCernicova-Dragomir I just needed to set the prop optional (?
), like this:
const Options = {
option1: 'option1' as 'options1',
option2: 'option1' as 'options2',
option3: 'option1' as 'options3',
}
type Option = typeof Options[keyof typeof Options];
type Dropdown = { [key in Option]?: string };
const obj: Dropdown = {
[Options.option1]: 'test'
}