Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda