Enums — TypeScript

enum makes named constants (fixed values that have a name), and each choice in it is called a member. A numeric enum is numbered from 0 (Status.Active = 0…

In older code, an order's status is stored as a number: 0 active, 1 inactive, 2 pending. Three months later you read if (status === 2) and can't remember what 2 means. If the status is text instead, someone always types "pendng" by mistake. JavaScript reports no error for either one.

An enum gives each of those choices a name. An enum is a set of named constants (fixed values that have a name), made with the enum keyword. So you write Status.Active or Status.Pending, not numbers or strings that are easy to mistype. Each choice inside an enum is called a member. Think of a desk fan's buttons: they say Off, Low, High, but inside, the machine uses the numbers 0, 1, 2. The word on a button is the name of an enum member, like Status.Active, and the number inside the machine is its value.

Numeric enums and reverse mapping

The enum Status below has three members, with no values:

enum Status {
  Active,   // 0
  Inactive, // 1
  Pending,  // 2
}

console.log(Status.Active); // 0
console.log(Status[0]);     // Active

Without values, enum members are numbered automatically starting at 0. An enum whose values are numbers is called a numeric enum. So Status.Active is 0, and in enum Color { Red, Green, Blue } the value of Color.Red is 0 too. If the first member gets a number, the rest continue from that number: enum Level { Low = 1, Medium, High } makes Level.High equal 3.

Types are removed when compiling, but an enum doesn't disappear: it becomes JavaScript code. This is the JavaScript tsc writes for Status:

var Status;
(function (Status) {
  Status[Status["Active"] = 0] = "Active";
  Status[Status["Inactive"] = 1] = "Inactive";
  Status[Status["Pending"] = 2] = "Pending";
})(Status || (Status = {}));

The outer part only creates an empty Status object, then fills it with the three lines in the middle. Take a guess first: how many keys (property names) does this compiled Status object have, 3 or 6?

String enums

In this version, every member of Status gets a string value, so it's called a string enum:

enum Status {
  Active = "active",
  Inactive = "inactive",
  Pending = "pending",
}

console.log(Status.Active); // active

But a parameter of type Status, for example in a function getStatusLabel, won't take a plain string, even one with exactly the same text:

getStatusLabel("active");
error TS2345: Argument of type '"active"' is not assignable to parameter of type 'Status'.

You have to write getStatusLabel(Status.Active).

String literal unions and as const objects

What's often used instead of an enum is a union of string literals, which you know from the Union & Literal lesson. This is the union version of Status:

type Status = "active" | "inactive" | "pending";

function getStatusLabel(status: Status): string {
  if (status === "active") return "Active";
  if (status === "inactive") return "Inactive";
  return "Waiting";
}

getStatusLabel("active"); // "Active"
getStatusLabel("done");   // error

The second call is rejected, because "done" isn't in Status:

error TS2345: Argument of type '"done"' is not assignable to parameter of type 'Status'.

Unlike with the string enum, here the right strings are accepted right away. When compiled, type Status is removed completely: 0 lines of JavaScript.

So your code can't read the list of choices in Status while the program runs, for example to fill a dropdown (a menu of choices). For that, use a plain object with as const:

const STATUS = {
  Active: "active",
  Inactive: "inactive",
  Pending: "pending",
} as const;

type Status = (typeof STATUS)[keyof typeof STATUS]; // "active" | "inactive" | "pending"

Why many teams pick unions

Almost every TypeScript feature is only types, which are removed when compiling. Enums are one of the few features that produce JavaScript code of their own. Because of that:

Enums are still valid and still common in older projects. For new code a union of literals is usually enough, plus an as const object when the list is also needed while the program runs.

⚠️ Numbers, text, and an enum that doesn't match

TL;DR: enum makes named constants (fixed values that have a name), and each choice in it is called a member. A numeric enum is numbered from 0 (Status.Active = 0, Color.Red = 0). When compiled, it becomes an object with reverse mapping: you can also read it from number to name (Status[0] = "Active"), so the 3 members of Status make 6 keys. A string enum gives each member its own string, so it's easier to read, but it won't take a plain string. A union of literals like type Status = "active" | "inactive" | "pending" disappears completely when compiled and accepts the right strings. An object with as const (its values locked to literal types) is used when the list is also needed while the program runs. Many teams pick unions, because enums are refused by the option erasableSyntaxOnly and Node can't run them directly.

⚡ A numeric enum starts at 0

Write:

enum Status {
  Active,   // 0
  Inactive, // 1
  Pending,  // 2
}

console.log(Status[0]); // Active
Guess first: when compiled, this enum becomes an object Status, and Status[0] gives Active. How many keys does that object have, 3 or 6?

There are 6 keys (property names): each member makes two keys, one from name to number (Active to 0) and one from number to name (0 to "Active"). The second direction is called reverse mapping.

Meaning: enum gives fixed names to a few choices, and each choice is called a member. Without values, the members are numbered automatically from 0 (a numeric enum). Unlike a type, an enum doesn't disappear when compiled.

⚡ A string enum is easier to read

Write:

enum Status {
  Active = "active",
  Inactive = "inactive",
  Pending = "pending",
}

Meaning: Every member has to get its own text. While debugging (finding the cause of a bug), logs and API data hold "active", not a 0 you have to match to a name yourself. But plain text is not accepted: with a function getStatusLabel(status: Status), getStatusLabel("active") is rejected, and you must write getStatusLabel(Status.Active).

⚡ A literal union instead

Write:

type Status = "active" | "inactive" | "pending";

getStatusLabel("active"); // "Active"
getStatusLabel("done");   // error

Meaning: Here Status is a union of three literal types. The right text is accepted right away, the wrong text is rejected, and the type disappears completely when compiled. An enum produces JavaScript of its own, so it can't simply be removed like a type: Node.js can't run it directly from a .ts file, and new Vite projects refuse it. That's why many teams pick unions for new code.

What you will learn