I have the following code, which gives me ts(1337)
error:
type Options = 'option1' | 'option2' | 'option3'
type ComplexValue = { [key: Options]: string } // ts error 1337
what I want to achieve here is that keys of ComplexValue
objects can be only one of Options
.
if I try to fix and define it differently by one of the below options:
type ComplexValue = { [K in Options]: string }
// or simply
type ComplexValue = Record<ServerFilterOperator, string>
and then trying to write something like this
const something: ComplexValue = { option1: 'ziv' }
then I get the below error
Type '{ [x: string]: string; }' is missing the following properties from type 'ComplexValue': option2, option3.ts(2740)
A workaround I can apply is adding ?
to the definition type ComplexValue = { [key in Options]?: string }
- this will apply that all keys are optional. But I actually wants to validate only single key:value
are passed.
What is the proper way validate that keys of ComplexValue
objects can be only one of Options
and only one key:value
pair is on the object?