Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

The enum keyword

Reference for TypeScript enum: numeric and string members, reverse mapping, computed members, enums as types, and the pitfalls to avoid.

An enum declares a named set of constants. Unlike most TypeScript features, an enum is not erased: the compiler emits a real JavaScript object, so an enum exists both as a type and as a value you can pass around, loop over and compare.

Members are either numbers (auto-incrementing from 0 unless you set them) or strings. Numeric enums also get a reverse mapping from number back to name; string enums do not. Many teams prefer a union of string literals or an as const object for simple cases, but enums remain common in existing code and are fine when you want a named, namespaced set of values.

TypeScript
enum Direction {
  Up,
  Down,
}

Try it

TypeScript Numeric enum and reverse mapping

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.

TypeScript String enums and type checking

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

TypeScript
enum Name {
  Member,                 // auto: previous + 1 (first is 0)
  Other = 5,              // constant initializer
  Text = "text",          // string initializer
  Computed = "abc".length // computed (numeric enums only)
}
declare enum Ambient { A = 1 }
const enum Inlined { A }
Part Description
Member name An identifier or a string literal. Names that are string literals must still be unique.
Initializer A literal, a reference to an earlier member, or a constant expression (1 << 2, `A
Auto value A member without an initializer gets the previous value plus one, which requires the previous member to be numeric and constant.

Edge cases and errors:

  • A member after a string member must have an explicit initializer; otherwise the compiler reports that the enum member must have an initializer.
  • Two declarations of the same enum in one scope merge, but only one may omit the initializer of its first member.
  • Passing an arbitrary string where a string enum is expected is an error; the enum type is opaque to string literals, as the second example shows.
  • In current TypeScript, numeric enums are checked strictly: assigning an out-of-range number literal to a numeric enum type is an error.

Usage notes

  • A numeric enum compiles to an object that holds both directions, so Object.keys(Level) lists numbers as well as names. String enums have only name-to-value entries.
  • Enum members are also types: Status.Active is a type usable in unions and discriminated unions when every member is a literal.
  • Heterogeneous enums (mixing strings and numbers) are legal but almost never a good idea.
  • Because enums emit code, they are not allowed under erasableSyntaxOnly and not supported by Node's type stripping. Use a const object with as const if you need pure erasable syntax.
  • Bit flags are a classic numeric-enum use: Read = 1 << 0, Write = 1 << 1, combined with |.
  • Prefer string enums for values that are logged, stored or sent over a network; their meaning survives reordering, while numeric values silently change when you insert a member.
  • const enum avoids the runtime object; see its own page for the trade-offs.