Enums
An enum declares a named set of related constants, such as the statuses an order can have. It is one of the few TypeScript features that is not erased — an enum compiles into a real JavaScript object that exists at runtime.
Detailed Explanation
Before enums, a status field was a bare string or a magic number, and a typo in "shiped" was found by a customer. An enum gives those values one declaration, one name, and one place to change.
enum Status { Pending, Shipped, Delivered }
Members with no initializer are numbered from zero and auto-increment, so Status.Pending is 0. You can set values explicitly, and numbering continues from the last number you gave.
Numeric enums also build a reverse mapping. The emitted object stores both Pending -> 0 and 0 -> "Pending", so Status[0] gives back the string "Pending". That is convenient for logging and surprising everywhere else, because it doubles the keys in the object.
String enums require every member to have a value and do not get a reverse mapping. They carry an unusual property for TypeScript: a string enum member is not interchangeable with the equivalent string literal. Currency.Usd accepts Currency.Usd only, never "usd", even though the runtime values are identical. In a language where assignability is otherwise structural, string enums are a deliberate island of nominal typing. That is either the feature you wanted or the thing that makes parsing JSON annoying.
Here is the part that separates enums from the rest of the type system. Almost everything else in TypeScript disappears at compile time. An enum does not. It emits an object wrapped in a function call, which means it occupies bundle space, resists tree-shaking (bundlers cannot easily prove which members are unused), and cannot be removed by a tool that only strips types.
That last point now matters practically. Runtimes that execute TypeScript by deleting type syntax — Node's type stripping, Deno, Bun — cannot handle a construct that needs code generation. TypeScript 5.8 added the erasableSyntaxOnly flag, which reports enums, namespaces with runtime members, and parameter properties as errors for exactly this reason.
So many teams now prefer one of two alternatives:
- A union of string literals:
type Status = "pending" | "shipped" | "delivered". Zero runtime cost, exhaustiveness checking in aswitch, and the values are ordinary strings. - An
as constobject: gives you a namespace to reference (Status.Pending) plus a derived union, still as plain data.
Use an enum when you want one named home for values with an explicit numeric mapping — wire protocol codes, database integers — and you are compiling with tsc. Prefer literal unions for the common case of "this field is one of these strings". TypeScript 5.0 also tightened numeric enums: every enum is now a union of its member types, so passing an arbitrary number where an enum is expected is an error rather than the old silent pass.
Code Examples
1. A numeric enum and the object it actually creates.
// 1. A numeric enum and the object it actually creates.
enum Status {
Pending, // 0
Shipped, // 1
Delivered = 10, // explicit
Returned, // 11, numbering continues from the last value
}
console.log(Status.Shipped); // 1
console.log(Status[10]); // "Delivered" — the reverse mapping
// The reverse mapping means the object has twice the keys you declared:
console.log(Object.keys(Status));
// ["0", "1", "10", "11", "Pending", "Shipped", "Delivered", "Returned"]
// Emitted JavaScript (abridged):
// var Status;
// (function (Status) {
// Status[Status["Pending"] = 0] = "Pending";
// Status[Status["Shipped"] = 1] = "Shipped";
// })(Status || (Status = {}));2. String enums behave nominally, which bites when data comes from outside.
// 2. String enums behave nominally, which bites when data comes from outside.
enum Currency {
Usd = "usd",
Inr = "inr",
}
function charge(amount: number, currency: Currency) {}
charge(500, Currency.Usd); // fine
charge(500, "usd");
// Error: Argument of type '"usd"' is not assignable to parameter of type 'Currency'.
// An API response is a plain string, so it needs a checked conversion.
const fromApi: string = "inr";
const safe = Object.values(Currency).includes(fromApi as Currency)
? (fromApi as Currency) // assertion is sound only because we checked first
: Currency.Usd;3. The zero-runtime alternatives most codebases reach for now.
// 3. The zero-runtime alternatives most codebases reach for now.
const Status = {
Pending: "pending",
Shipped: "shipped",
Delivered: "delivered",
} as const; // 'as const' keeps the literal types instead of widening to string
type Status = (typeof Status)[keyof typeof Status];
// "pending" | "shipped" | "delivered"
function label(s: Status): string {
switch (s) {
case Status.Pending:
return "Waiting";
case "shipped": // both spellings work; the values are ordinary strings
return "On its way";
case Status.Delivered:
return "Done";
}
}Common Gotchas
Enums are not erased like the rest of TypeScript
each one emits an object into your bundle and cannot be reliably tree-shaken. If you are targeting a type-stripping runtime or have enabled `erasableSyntaxOnly`, enums are a compile error.
Reverse mappings pollute iteration
`Object.keys`, `Object.values`, and `for...in` over a numeric enum return both the names and the numbers. Code that loops over a numeric enum to build a dropdown will produce twice as many options as expected. String enums do not have this problem.
Renumbering a numeric enum is a data migration
the numbers are what gets stored in your database and sent over the wire. Inserting a member in the middle of the list silently shifts every value after it, and yesterday's persisted rows now mean something else. Assign values explicitly if they leave the process.
String enum members are nominal, not structural
you cannot pass `"usd"` where `Currency` is expected, so every value parsed from JSON needs a runtime check plus an assertion. A literal union avoids the step entirely.