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

253
Vistas
Typescript: How to use key type as enum in an object keeping all enum values as optional?

I am using a enum type as key for an object:

enum Level {
  Novice, Intermediate, Pro
}

type Players = { [key in Level] : string[]}

Upon trying to create an object of type players with just Novice and Pro, I get the error saying Intermediate is not present:

const mapping: Players = {
  [Level.Novice]: ["Neo"],
  [Level.Pro]: ["John Wick"]
}

How do I make it work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I found out that this can be done by marking the key as optional in the type

type Players = { [key in Level]? : string[]}

Please notice the '?' which lies outside the []

I thought it was a good learning so I shared by answering my own question.

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