TypeScript Reference ยท Classes, Enums and Decorators
TypeScript numeric enums
Reference for TypeScript numeric enums: auto-incrementing members, explicit initializers, reverse mapping from value to name, and the pitfalls of number assignability.
A numeric enum is a named set of number constants. If you write an enum without initializers, the first member is 0 and every following member is one larger than the one before. Because the result is a real JavaScript object, you can use the enum both as a type (Direction) and as a value (Direction.Up) at run time.
Numeric enums also get a reverse mapping: the compiled object maps each name to its number and each number back to its name. That makes it easy to print a readable label from a stored number.
enum Direction { Up, // 0 Down, // 1 Left, // 2 Right, // 3 }
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 Name { Member, // auto: previous + 1 (first is 0) Other = 5, // explicit initializer Next, // 6 Computed = "abc".length // computed; the member after it needs an initializer }
| Part | Description |
|---|---|
Name |
The enum name. It becomes a type (the union of its members) and a value (the object). |
Member |
A member name. Identifiers or string literals such as "two words" are allowed; numeric names are not. |
| Initializer | An optional constant or computed number. Without one, the value is the previous value plus 1, or 0 for the first member. |
Returns: reading Name.Member gives a number; reading Name[number] gives the member name as a string (typed as string, even if the number has no member).
Edge cases:
- A member that follows a computed member must have its own initializer, because the compiler cannot know the previous value.
- Two members may share a value; the reverse mapping then returns the name declared last.
Usage notes
- The compiled object looks like
{ "0": "Up", Up: 0, ... }. This is whyObject.keyson a numeric enum lists numbers and names, and whyfor...inloops over it need a filter. - Numeric enums are loosely typed: a value of type
numberis assignable to an enum type. A numeric literal that matches no member (such as42forSize) is rejected with TS2322, but a variable typednumberslips through, so validate numbers that come from outside. If you need strict values, use a union of literals or a string enum. - Reverse mapping is only generated for numeric members. String members have none, so a mixed enum has a partial reverse map (see heterogeneous enums).
- Plain enums are not erasable syntax. They emit code, so they need a compile step and fail under
erasableSyntaxOnlyor Node's type stripping. - For bit flags, give each member a power of two and combine them with
|, test with&. - Enums are also namespaces: you can merge extra functions into an enum with
namespace Level { ... }declarations of the same name.