Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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: ""
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!