0

I'm using typescript 3.7 Given

enum InformationSection {
  Education = 'Education label',
  Resume = 'Resume label'
}

the code

InformationSection["Education"]

returns 'Education label' and it's a valid statement but if I write

Object.keys(InformationSection).map(x => InformationSection[x])

then I get the following error:

Property 'Education label' does not exist on type 'typeof InformationSection'

what's wrong here?

8

2 Answers 2

0

The only way that I found is to trick typescript to make it stop complain about the invalid index type

(InformationSection as any)[x]
Sign up to request clarification or add additional context in comments.

1 Comment

Any better approach?
0
Object.keys(InformationSection).map(x => InformationSection[x keyof typeof InformationSection])

String enums are tricky as they cannot be reverse mapped easily: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html#string-enums

Source: https://stackoverflow.com/a/66683328

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.