2

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

Ziv Levy
  • 1,904
  • 2
  • 21
  • 32
  • 1
    Does this answer your question? [Typescript type where an object consists of exactly a single property of a set of possible properties](https://stackoverflow.com/questions/62158066/typescript-type-where-an-object-consists-of-exactly-a-single-property-of-a-set-o) – Tobias S. Aug 04 '22 at 10:04
  • @TobiasS. checking... – Ziv Levy Aug 04 '22 at 10:59

1 Answers1

3

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: ""
};
Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • thanks @Behemoth - can you please elaborate and explain each of the operators. I'm relatively new with TS and I'm hard to understand how this magic works – Ziv Levy Aug 04 '22 at 11:07
  • I also have the following situation, where the key of `complex1` vairable is dynamically set. This prompts another TS error. see the playground in the next comment: – Ziv Levy Aug 04 '22 at 11:24
  • https://www.typescriptlang.org/play?#code/C4TwDgpgBA8mwEsD2A7AzlAvFA5E+yKAjDlAD676KoBMpFeBqAzDgFBuiRQBKEAjgFcEAJwgBlBCgDmAGwgAeANJQIAD2AQUAEwwAFEfggjQSiCAB8WKAG82UKAG09UKVCUBdAFy3HANVcUKD1vKDRgESlpAF8oADJggEMTBETZBXsHXggAYyQRbQUAUTUc2UFtRSUAGmCLWpQIADdjC0yrdU0dDCkAM2MoABVMhwB+XwANQKgAa3MkXqHQwccJj2iRqB9GlpEAbjZox08DznBoAGEkAFsweTU-NMFobD4hUQko+QU4anQLU55dDAKDXRJgABihmufiIwCQfhoPj4eQKChQgmuACNjLVfoQ0FZsHYslAiD5GH8SNVNkjKEwUHQaaTmBSqIRWDSNmwgeEwjcIMAABZRHxXW73R7lF62JxgyHQ2HwxGOIgeUI4ABeCCapGiQA – Ziv Levy Aug 04 '22 at 11:24