Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!