Sitemap
Kolos Farkas

Kolos Farkas

Software architect, frontend ninja, node monkey during the days. One man project during the nights.

Enumeration in Typescript

4 min readJun 6, 2022

--

Press enter or click to view image in full size

As software engineers we cannot avoid enumerations in our projects. Enumeration is a language independent solution to store a value with a string representation.

In different languages we have different solutions to do that:

C#

C# is the most powerful backend language developed by Microsoft. It has a lot of powerful stuff like enums.

public enum Color {
Red = 1;
Green = 2;
Blue = 3;
}

In C# an enum represents a numeric value, could be cast into a string with the .ToString() extension method, and sometimes an enum could contain more than one value.

var redOrGreen = Color.Red | Color.Green;

But in the above chase the [Flags] attribute should be added to the enum.

JavaScript

Javascript does not have native support for enum, but there are ways to do it.

Enum as const properties
In this case we are creating public static properties on a class. We can use the properties without creating the class instance. Not the best solution but it could work.

class Color {
static Red = 1;
static Green = 2;
static Blue = 3;
}

As I mentioned earlier, we not love this solution, because JS is a prototype based OOP language, which means it doesnt like the static keyword. To avoid that we should find a better solution.

Enum as an object
Instead of creating an antipatternish class with static properties we could create an object, and use it as an enum.

const Color = Object.freeze({
Red : 1,
Green: 2,
Blue: 3
});

In the code above I’ve used the Object.freeze() utility function to mark my object as readonly. The object cannot be modified in runtime.
We could use the above object as an enum.

In both JS examples we could use different types in the enum. Not just primitives such as string, number and bool, but complex object types such as object and array.

TypeScript

In TS we could use both the JS approaches, but we have native enum support in TS. Lets see the possibilites with TypeScript enums.

Get Kolos Farkas’s stories in your inbox

Join Medium for free to get updates from this writer.

export enum
Export enum is a very easy way to create an enum in typescript which exists in runtime as well.

export enum Color {
Red = 1,
Green = 2,
Blue = 3
};

But, the code above will compile into this:

export var Color;
(function (Color) {
Color[Color["Red"] = 1] = "Red";
Color[Color["Green"] = 2] = "Green";
Color[Color["Blue"] = 3] = "Blue";
})(Color || (Color = {}));

So, we will have an object Color with the defined properties (Red , Green and Blue), but the object will have the values as properties as well (1 , 2 and 3).
If we log the Color object, we will get this:

Figure 1 — Enum logged in runtime

So, in short, export enum in Typescript is very good, if we are creating a library, as we need the enum in runtime. But in most cases, we are creating enums to store a type such as gender. Do we need that information in runtime? The answer is no. We could “hardcode” it into our code.

export const enum
This is the another approch in Typescript. We can create an export const enum, which contains the data we need, but it wont be available in runtime. Lets create a const enum:

export const enum Color {
Red = 1,
Green = 2,
Blue = 3
}

This code wont be compiled into Javascript:

Press enter or click to view image in full size
Figure 2 — Const enum compilation result

This means, if we create a const enum, it wont be a part of our bundle by default, so we have to use it.

const red = Color.Red;
const green = Color.Green;
const blue = Color.Blue;

If we compile the code above, we will get something like this:

Figure 3 — Const enum usage

This means, if we have a const enum in our code, then we can think on it as a hardcoded value. The best approach is project-dependent as nearly everything in software development.

Plus one: types
In Typescript we could define a type which will behave like a const enum. types wont be a part of our distribution (javascript) file, but as the const enums they could be found in the *.d.ts files.

export type Color = "Red" | "Green" | "Blue";

As you can see above, a type cannot carry any additional information. In some cases it worths to use a type instead of a const enum, but it makes the code harder to read and maintain. I’d suggest to use a custom type. When the related code wont be extended. But there is the question again:

Do we need to chain down our hands with types?

--

--

Kolos Farkas
Kolos Farkas

Software architect, frontend ninja, node monkey during the days. One man project during the nights.