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

90
Vistas
TypeScript Optional Sub-Properties

So I know that there is a way to create a type-alias to have properties such as answer I have seen from another user in this post (Typescript interface optional properties depending on other property) like below:

type Sample = {
  key1: true,
  key2?: string,
  key3: number
} | {
  key1: false,
  key2?: string,
  key3?: never
}

but how would I do it if the dependent properties are inside (as in, both key2 and key3 belong to key1, but key3 would exist only if key2 is true)? Would it be something like this:

type Sample = {
  key1: {
  key2?: true,
  key3: number
 }
} | {
  key1: {
  key2?: false,
  key3?: never
 }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

An option is to just make the distinction inside like this:

type Sample = {
  key1: {
  key2?: true,
  key3: number
 } | {
  key2?: false,
  key3?: never
 }
}

Your code actually works too

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