TypeScript Reference ยท Primitive and Special Types
TypeScript enum types
Reference for how enums act as types in TypeScript: enum member types, union enums, numeric vs string enums, reverse mapping, keyof typeof and const enums.
An enum declaration creates two things at once: a runtime object that holds the members, and a type with the same name. That is unusual in TypeScript, where most type features vanish after compilation. Because an enum exists at runtime, it is one of the few places where TypeScript adds JavaScript output.
When every member is a literal (a number or a string), each member also becomes its own type, and the enum type is the union of its members. That makes enums narrow and checkable, for example in a switch. This page covers enums as types; the declaration syntax is on the keyword page and in the lessons.
enum Color { Red, Green } let c: Color.Red = Color.Red;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
enum E { A, B } // numeric, 0 and 1 enum S { A = "a", B = "b" } // string const enum C { A = 1 } // inlined at compile time declare enum D { A } // ambient type Member = E.A; // member type type Names = keyof typeof E; // "A" | "B"
| Part | Description |
|---|---|
E (as a type) |
The union of all members of E when all are literal members. |
E.A (as a type) |
The type of the single member A. |
typeof E |
The type of the runtime object. |
keyof typeof E |
Union of the member names as strings. |
Edge cases:
- Since TypeScript 5.0 every enum is a union enum, and assigning a numeric literal that is not a member value is an error. A value typed as plain
numberis still assignable to a numeric enum. - Numeric members get a reverse mapping (
Level[1] === "Mid"), soObject.keyslists both names and numbers. - String enum values are not interchangeable with plain string literals, even with identical text.
- With
isolatedModulesorerasableSyntaxOnly,const enumand enums can be restricted; some tools forbid enums entirely.
Usage notes
- Alternative: a const object with
as constplus a derived union gives enum-like behavior with no extra emitted code. - Exhaustiveness. Switching on a union enum lets TypeScript check that every member is handled when the result is typed.
- Namespaces merging. Enums can merge with a namespace to add helper functions.
const enumcaveats. Values are inlined, so there is no runtime object; it is a poor fit for libraries andisolatedModules.- Heterogeneous enums mixing strings and numbers are legal but rarely a good idea.
- Serialization. String enums survive JSON round trips readably; numeric ones depend on member order.
Related
- Enums (keyword), String literal types,
keyof,typeof - Lessons: Enums, Literal types