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

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

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 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!