Check if a Value exists in an Enum in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
3 min

banner

# 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'); }

check if value exists in an enum in typescript

The code for this article is available on GitHub

If you use numeric enums, use the following code sample instead.

index.ts
// โœ… For Numeric Enums enum SizesNumeric { Small, Medium, Large, } if (2 in SizesNumeric) { console.log(SizesNumeric[2]); // ๐Ÿ‘‰๏ธ "Large" }

check if value exists in numeric enum

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.

# Check if a Value exists in a Numeric Enum

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 code for this article is available on GitHub

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 can 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

get key of numeric enum from value

Unfortunately, you can only use this approach to get the 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev