Skip to main content

What is enum in TypeScript?

What enum is in TypeScript

enum (enumeration) is a special type in TypeScript that lets you define a set of named constants.

It is used when you need to represent a fixed set of possible values, for example roles, statuses, directions, and so on.


Why enum is needed

Instead of writing "magic strings" or numbers by hand:

javascript
if (status === "loading") { ... }

you can use named values, which:

  • makes the code more readable,
  • reduces the risk of typos,
  • improves autocomplete and type checking.

How to declare an enum

Example 1. Numeric enum (default)

javascript
enum Direction { Up, Down, Left, Right, } let move: Direction = Direction.Up; console.log(move); // 0

By default TypeScript assigns values starting from 0 (Up = 0, Down = 1, ...).


Example 2. Setting your own numeric values

javascript
enum StatusCode { OK = 200, NotFound = 404, ServerError = 500, } console.log(StatusCode.OK); // 200 console.log(StatusCode[404]); // "NotFound"

Numeric enums are two-way: you can get the name from the value, and the value from the name.


Example 3. String enum

javascript
enum Direction { Up = "UP", Down = "DOWN", Left = "LEFT", Right = "RIGHT", } console.log(Direction.Left); // "LEFT"

Unlike numeric ones, string enums are one-way: you cannot get the key from the value.


javascript
enum Mix { Yes = 1, No = "NO", }

This variant exists, but it hurts readability and gets in the way of autocomplete. It is better to avoid mixed types.


How to use enum

In variables:

javascript
let currentDirection: Direction = Direction.Right;

In functions:

javascript
function movePlayer(direction: Direction) { switch (direction) { case Direction.Up: console.log("Going up"); break; case Direction.Down: console.log("Going down"); break; } }

In interfaces:

javascript
enum UserRole { Admin, User, Guest, } interface User { name: string; role: UserRole; } const tim: User = { name: "Tim", role: UserRole.Admin, };

Automatic numbering

TypeScript numbers the values itself if you did not specify them manually:

javascript
enum Priority { Low, // 0 Medium, // 1 High, // 2 }

You can set a starting value - the rest will follow in order:

javascript
enum Priority { Low = 1, // 1 Medium, // 2 High, // 3 }

Compilation to JavaScript

At compile time, TypeScript turns an enum into a regular object:

javascript
var Direction; (function (Direction) { Direction[(Direction["Up"] = 0)] = "Up"; Direction[(Direction["Down"] = 1)] = "Down"; })(Direction || (Direction = {}));

This allows reverse mapping (numeric enums are two-way).


Alternative: const enum

If you do not need two-way mapping and want maximum performance, use const enum: it is removed at compile time and does not create an object in the JS code:

javascript
const enum Direction { Up, Down, Left, Right, } const dir = Direction.Up; console.log(dir); // 0

It simply compiles to:

javascript
const dir = 0 /* Up */;

Summary

Enum kindExampleValuesReverse mapping
Numericenum A { X, Y }0, 1, ...Yes
Stringenum A { X = "x" }"x"No
Heterogeneousenum A { X = 1, Y = "y" }1, "y"Not recommended
const enumconst enum A { X, Y }inlined into the codeNo (inline)

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.