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:
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)
enum Direction {
Up,
Down,
Left,
Right,
}
let move: Direction = Direction.Up;
console.log(move); // 0By default TypeScript assigns values starting from
0(Up = 0, Down = 1, ...).
Example 2. Setting your own numeric values
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
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.
Example 4. Heterogeneous (mixed) enum (not recommended)
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:
let currentDirection: Direction = Direction.Right;In functions:
function movePlayer(direction: Direction) {
switch (direction) {
case Direction.Up:
console.log("Going up");
break;
case Direction.Down:
console.log("Going down");
break;
}
}In interfaces:
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:
enum Priority {
Low, // 0
Medium, // 1
High, // 2
}You can set a starting value - the rest will follow in order:
enum Priority {
Low = 1, // 1
Medium, // 2
High, // 3
}Compilation to JavaScript
At compile time, TypeScript turns an enum into a regular object:
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:
const enum Direction {
Up,
Down,
Left,
Right,
}
const dir = Direction.Up;
console.log(dir); // 0It simply compiles to:
const dir = 0 /* Up */;Summary
| Enum kind | Example | Values | Reverse mapping |
|---|---|---|---|
| Numeric | enum A { X, Y } | 0, 1, ... | Yes |
| String | enum A { X = "x" } | "x" | No |
| Heterogeneous | enum A { X = 1, Y = "y" } | 1, "y" | Not recommended |
const enum | const enum A { X, Y } | inlined into the code | No (inline) |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.