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?
Status["Active"] = 0sets the keyActiveto 0. An assignment with=also produces the value it assigns (0), and that 0 is used again as a key:Status[0] = "Active".- So you can read this object in two directions: from name to number (
Status.Activeis 0), and from number to name (Status[0]is"Active"). The number-to-name direction is called reverse mapping. Object.keys(Status)(the list of an object's keys) gives["0", "1", "2", "Active", "Inactive", "Pending"]: 6 keys, not 3.
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
- In a string enum, every member must get its own string value.
- There's no reverse mapping:
Object.keys(Status)is only["Active", "Inactive", "Pending"]. - The advantage is that it's easier to read: while debugging (finding the cause of a bug), logs and API data say
"active", not a0you have to match to a name yourself.
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"
as consttells TypeScript this object will never change. Each value gets a literal type ("active", notstring), and every property becomesreadonly.typeof STATUS, written where a type goes, takes the type of the objectSTATUS.keyoftakes its key names, and[...](an indexed access type) takes the type of its values:"active" | "inactive" | "pending".- The JavaScript stays a plain object, with no reverse mapping.
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:
- Recent versions of Node.js (22.18 and later, including 24) can run a
.tsfile directly, by stripping (removing) its types. An enum can't simply be stripped, so Node stops:SyntaxError [ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX]: TypeScript enum is not supported in strip-only mode(the mode that only removes types). An older project full of enums can run withnpx tsx file.ts: tsx turns enums into JavaScript first. - The option
erasableSyntaxOnly(since TypeScript 5.8) in tsconfig (the TypeScript settings file) forbids all syntax that can't simply be removed. With this option enabled, everyenumis refused:This syntax is not allowed when 'erasableSyntaxOnly' is enabled.(ts(1294)). Unions andas constobjects are still accepted. The React, Vue and Vanilla (without React or Vue) TypeScript templates ofnpm create vite@latestalready enable this option, so in a new Vite project,enumis refused.
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
- A numeric enum for data that is text. API data holds
"active", whileStatus.Activein a numeric enum is 0, sostatus === Status.Activeis alwaysfalse. If the data is text, use a string enum or a union. - Passing a plain string to an enum parameter.
getStatusLabel("active")is refused when the parameter is a string enum. WriteStatus.Active, or use a union. - Forgetting values in a string enum. Once one member gets a string, every member after it needs a value too.
- Getting the third label wrong. In the exercise,
"pending"means still waiting, so its label is"Waiting", not a rejection like "Rejected".
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.