Check if a Value exists in an Enum in TypeScript

avatar

Borislav Hadzhiev

Last updated: Feb 14, 2022

banner

Photo from Unsplash

Check if a Value exists in an Enum in TypeScript #

To check if a value exists in an enum:

  1. Use the Object.values() method to get an array of the enum's values.
  2. Use the includes() method to check if the value exists in the array.
  3. The includes method will return true if the value is contained in the enum and false otherwise.
index.ts
// ✅ For String Enums enum Sizes { Small = 'S', Medium = 'M', Large = 'L', } const keys = Object.keys(Sizes); console.log(keys); // 👉️ ['Small', 'Medium', 'Large'] const values = Object.values(Sizes); console.log(values); // 👉️ ['S', 'M', 'L'] if (values.includes('S' as unknown as Sizes)) { console.log('✅ Value exists in enum'); } // ✅ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } if (2 in SizesNumeric) { console.log(SizesNumeric[2]); // 👉️ "Large" }

Enums in TypeScript are real objects and exist at runtime. This is why we are able to pass an enum to the Object.keys and Object.values methods.

The methods return an array containing the object's keys and values, on which we can use the includes method to check if a specific value is contained in the enum's values.

The second example shows how to check if a specific value is contained in a numeric enum.

index.ts
// ✅ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } if (2 in SizesNumeric) { console.log(SizesNumeric[2]); // 👉️ "Large" }

The numeric enum in the example has no initial values, so the first value is 0.

We are able to do this because TypeScript provides reverse mappings for numeric enums.

In other words, we are able to get the key of a numeric enum from the corresponding value.

index.ts
enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // 👉️ Small console.log(SizesNumeric[1]); // 👉️ Medium

Unfortunately, you can only use this approach to get tha names of numeric enums.

String enum members don't get a reverse mapping generated at all.

The Object.keys() and Object.values() methods also work differently with numeric enums - the returned array contains the keys and the values.

index.ts
// ✅ For Numeric Enums enum SizesNumeric { Small, Medium, Large, } const keys = Object.keys(SizesNumeric); console.log(keys); // 👉️ ['0', '1', '2', 'Small', 'Medium', 'Large'] const values = Object.values(SizesNumeric); console.log(values); // 👉️ ['Small', 'Medium', 'Large', 0, 1, 2]

If you need an array that only contains the keys or values, you can filter out the unnecessary values.

index.ts
enum SizesNumeric { Small, Medium, Large, } const keys = Object.keys(SizesNumeric).filter((v) => isNaN(Number(v))); console.log(keys); // 👉️ ['Small', 'Medium', 'Large'] const values = Object.values(SizesNumeric).filter((v) => !isNaN(Number(v))); console.log(values); // 👉️ [0, 1, 2] if (values.includes(2)) { console.log('2 is contained in the values of the enum'); }

Note that the examples above would not work if you use const enums, because const enums can only use constant enum expressions and are completely removed during compilation.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.