How to use a Switch statement with Enums in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
2 min

banner

# Use a Switch statement with Enums in TypeScript

To use a switch statement with enums:

  1. Create a reusable function that takes an enum value as a parameter.
  2. Use a switch statement and switch on the provided value.
  3. Return a specific value from each branch.
index.ts
enum Sizes { Small, Medium, } function getSize(size: Sizes) { switch (size) { case Sizes.Small: console.log('small'); return 'S'; case Sizes.Medium: console.log('medium'); return 'M'; default: throw new Error(`Non-existent size in switch: ${size}`); } } console.log(getSize(Sizes.Small)); // ๐Ÿ‘‰๏ธ "S" console.log(getSize(Sizes.Medium)); // ๐Ÿ‘‰๏ธ "M"

use switch statement with enums

The code for this article is available on GitHub

We created a reusable function that takes an enum value as a parameter, switches on the enum value before returning something else.

The default case is a matter of implementation. In the example, we throw an error to indicate that an unexpected state has occurred - the passed-in value is not present in the enum.

# Use a Switch statement with Numeric Enums in TypeScript

If you have a numeric enum and you try to use a switch statement directly, you might get an error that "Type X is not compatible to type Y".

In this scenario, you can convert the enum value to a number when switching on it.

index.ts
enum Sizes { Small, Medium, } switch (Number(Sizes.Small)) { case Sizes.Small: // ๐Ÿ‘‡๏ธ this runs console.log('size is S'); break; case Sizes.Medium: console.log('size is M'); break; default: console.log(`non-existent size: ${Sizes.Small}`); break; }

use switch statement with numeric enums

The code for this article is available on GitHub

Had we not converted the enum value to a number when switching on it, we would get an error that the two types are not compatible.

Make sure to always use the break keyword to avoid a fallthrough switch which could run multiple code blocks.

If you're in a function, you will most likely use return instead of break.

# Use conversion to String if necessary

If you are getting a similar error when working with string enums, you can convert the value to a string in the switch statement.

index.ts
enum Sizes { Small = 'S', Medium = 'M', } switch (String(Sizes.Small)) { case Sizes.Small: console.log('size is S'); break; case Sizes.Medium: console.log('size is M'); break; default: console.log(`non-existent size: ${Sizes.Small}`); break; }

use conversion to string if necessary

The code for this article is available on GitHub

If you run the code from the example above, the size is S message gets logged to the console.

If you get the Conversion of type 'X' to type 'Y' may be a mistake in TS error, click on the link and follow the instructions.

If you need to check if a value exists in an enum, click on the following article.

# 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