Categories
TypeScript Answers

How to map Typescript enums?

Spread the love

Sometimes, we want to map Typescript enums.

In this article, we’ll look at how to map Typescript enums.

How to map Typescript enums?

To map Typescript enums, we get the keys and map them.

For instance, we write

const mapped = (Object.keys(MyEnum) as Array<keyof typeof MyEnum>).map(
  (key) => {
    //...
  }
);

to call Object.keys with MyEnum to return an array of enum keys.

Then we call map with a callback to return an array with the mapped entries.

Conclusion

To map Typescript enums, we get the keys and map them.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *