Last updated: Feb 26, 2024
Reading timeยท3 min
To check if a value exists in an enum:
Object.values()
method to get an array of the enum's values.includes()
method to check if the value exists in the array.includes
method will return true
if the value is contained in the
enum and false
otherwise.// โ 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'); }
If you use numeric enums, use the following code sample instead.
// โ 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.
// โ 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 can get the key of a numeric enum from the corresponding value.
enum SizesNumeric { Small, Medium, Large, } console.log(SizesNumeric[0]); // ๐๏ธ Small console.log(SizesNumeric[1]); // ๐๏ธ Medium
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.
// โ 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.
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.
You can learn more about the related topics by checking out the following tutorials: