Convert an Enum to a String or a Number in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
3 min

banner

# Convert an Enum to a String or a Number in TypeScript

To convert a numeric enum to a string, use bracket notation to access a specific value on the enum to get its name, e.g. NumericEnum[NumericEnum.Yes].

Similarly, you can convert a string enum to a string by using dot notation to access a specific property.

index.ts
// โœ… NUMERIC Enums enum NumericEnum { Yes, No, Maybe, } // ๐Ÿ‘‡๏ธ to string const str = NumericEnum[NumericEnum.Yes]; console.log(str); // ๐Ÿ‘‰๏ธ "Yes" // ๐Ÿ‘‡๏ธ to number const num = NumericEnum.Yes; console.log(num); // ๐Ÿ‘‰๏ธ 0

convert enum to string or number

The code for this article is available on GitHub

If you have a String Enum, use the following code sample instead.

index.ts
// โœ… STRING Enums enum StringEnum { Yes = 'Y', No = 'N', Maybe = 'M', } // ๐Ÿ‘‡๏ธ to string const str = StringEnum.Yes; console.log(str); // ๐Ÿ‘‰๏ธ "Y"

converting string enum to string

The first enum in the example is a numeric enum, where the Yes property points to a value of 0, No points to a value of 1 and Maybe to a value of 2.

To convert a numeric enum to a string, we have to get the name for a specific value.

# Converting a Numeric enum to a string

With numeric enums, we can use a reverse mapping to access the name of a specific value.

index.ts
enum NumericEnum { Yes, No, Maybe, } // ๐Ÿ‘‡๏ธ Both lines do the same console.log(NumericEnum[0]); // ๐Ÿ‘‰๏ธ "Yes" console.log(NumericEnum[NumericEnum.Yes]); // ๐Ÿ‘‰๏ธ "Yes"

converting numeric enum to string

The code for this article is available on GitHub

Passing a specific value of the enum between the brackets enables us to convert it to a string by getting the corresponding name.

I've also written an article on how to convert a string to an enum in TS.

# Converting a Numeric enum to a number

If you need to convert a numeric enum to a number, access a specific property using dot notation.

index.ts
// โœ… NUMERIC Enums enum NumericEnum { Yes, No, Maybe, } // ๐Ÿ‘‡๏ธ to number const num = NumericEnum.Yes; console.log(num); // ๐Ÿ‘‰๏ธ 0 console.log(typeof num); // ๐Ÿ‘‰๏ธ "number"

convert numeric enum to number

The code for this article is available on GitHub

# Converting a string enum to a string

Similarly, you can convert a string enum to a string by accessing one of the names in the enum using dot notation.

index.ts
// โœ… STRING Enums enum StringEnum { Yes = 'Y', No = 'N', Maybe = 'M', } // ๐Ÿ‘‡๏ธ to string const str = StringEnum.Yes; console.log(str); // ๐Ÿ‘‰๏ธ "Y"
The code for this article is available on GitHub

Note that reverse mappings don't work for string enums. String enum members do not get a reverse mapping generated at all.

Enums are real objects in TypeScript, so you can use the Object.keys() and Object.values() methods to get an array of the enum's names or values.

index.ts
// โœ… STRING Enums enum StringEnum { Yes = 'Y', No = 'N', Maybe = 'M', } const names = Object.keys(StringEnum); console.log(names); // ๐Ÿ‘‰๏ธ ['Yes', 'No', 'Maybe'] const values = Object.values(StringEnum); console.log(values); // ๐Ÿ‘‰๏ธ ['Y', 'N', 'M']

This works similarly with numeric enums.

index.ts
// โœ… NUMERIC Enums enum NumericEnum { Yes, No, Maybe, } const names = Object.keys(NumericEnum).filter((v) => isNaN(Number(v))); console.log(names); // ๐Ÿ‘‰๏ธ ['Yes', 'No', 'Maybe'] const values = Object.values(NumericEnum).filter((v) => !isNaN(Number(v))); console.log(values); // ๐Ÿ‘‰๏ธ [0, 1, 2]

However, the Object.keys() method returns an array containing the enum's names and values, so you have to use the filter method to filter out the unnecessary values.

# 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