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

107
Vistas
Dynamically create an object with keys based on type

I have this code:

type Foo = {
  fiz: 'baz',
  bar: 'biz'
}

const foo: Partial<Foo> = {}

Essentially, the type will change a lot, and I want to do smth like this:

Object.keys(Foo).map(key=>{
  foo[key] = null;
})

But that's obviously impossible. Is there a way to make it work dynamically? Or am I stuck with typing each key dynamically?

Link to playground

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

0

Im not entirely sure what your goal is. Are you trying to ensure your objects will always start with all keys, even if they have no values?. I dont think this would be possible based on the type due to the type not existing at runtime. Instead you could create an object of that type with all keys and use the spread operator {...foo} to create other objects with those keys (Example).

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