Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda