1

Is there a stricter way of typing

an array build with Object.keys ?

I mean instead using string[]

sort of (key in Languages)[] ?

export enum Languages {
    de = 'de',
    en = 'en',
    fr = 'fr'
}

const langs: string[] = Object.keys(Languages);
1
  • 1
    Just a side note: enum names are usually singular rather than plural, e.g. Language rather than Languages. Commented Dec 14, 2019 at 13:03

1 Answer 1

2

Object.keys is defined as returning string[] because it needs to be defined to handle the general case.

In your specific case, you can use a type assertion on the value keys returns:

const langs = Object.keys(Languages) as Languages[];

Live on the playground

Sign up to request clarification or add additional context in comments.

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.