Search code examples
javascripttypescripttypeerror

Typescript: validate that keys of Type objects are only one of K and single key:value pair


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?

link to TS playground is here


Solution

  • It's a little tricky but you can get it done. Inspired by a few other answer I came up with this:

    type RequireSingle<K extends PropertyKey> = {
      [P in K]: {[V in P]: string} & Partial<
        Record<Exclude<K, P>, never>
      > extends infer T
        ? {[X in keyof T]: T[X]}
        : never;
    }[K];
    
    type ComplexValue = RequireSingle<Options>;
    const complex: ComplexValue = {};
    // .  ~~~~~~~~ Type '{}' is not assignable to type 'ComplexValue'.
    const complex1: ComplexValue = {
      option1: ""
    };
    const complex2: ComplexValue = {
      //  ~~~~~~~~ Type '{ option1: string; option2: string; }' is not assignable
      //           to type 'ComplexValue'.
      option1: "",
      option2: ""
    };