Enums allows us to define a set of named constants. By using enums we can keep our code DRY, easy to refactor, and more readable.

For example, if we need to use set of colors in our application that represents a numeric value in the server, we can do the following:

Enum

Enums are not magic. If we take a look at the transpiled code, we can see that it is just a plain JS object.

Enums Output

The disadvantage here is that all of this code goes into our JS bundle file even though we do not use it in a dynamic way, which leads us to three problems:

The bundle will increase depending on the number of enums. Increased memory consumption. We need to perform a lookup each time we access the value.

In most cases, what we really want is to simply replace the object with the specific value we need. In fact, in typescript we can do this by using const enums.

The only thing that we need to do is add a const before the enum definition.