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

188
Views
Looping through enum in react typescript render gives error
  interface ColorThemes {
    DEFAULT: string,
    DARK: string,
  }

  const colorThemes: ColorThemes = {
    DEFAULT: "default",
    DARK: "dark",
  }

  return (
    <div>
      {Object.keys(colorThemes).map(key =>
        <div>{colorThemes[key]}</div>
      )}
    </div>
  )

I'm trying to loop through an enum of strings. But visual studio intellisense complains and says:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'xxx'.
  No index signature with a parameter of type 'string' was found on type 'xxx'.

Any help would be appreciated.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Object.keys(colorThemes) gives you a list of strings, but at colorThemes[key] the key must be a ColorThemes-key, not just any string.

But because you know for sure that every key is always a key of colorThemes, you can tell Typescript to assume that:

<div>
  {Object.keys(colorThemes).map(key =>
    <div>{colorThemes[key as keyof ColorThemes]}</div>
  )}
</div>
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!