Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript enum computed and constant members

Reference for TypeScript enum member values: which initializers count as constant, which are computed, how they affect union enums, and the rules after computed members.

Every enum member is either constant or computed. A member is constant when the compiler can work out its value while type-checking: no initializer, a numeric or string literal, a reference to an earlier constant member, or a simple expression built from those with operators such as +, |, << and ~.

Anything else is computed: its value is calculated when the program runs. Computed members work, but they give up some static guarantees, and they cost you the "union enum" type behavior for the enum as a whole.

TypeScript
enum Example {
  A,            // constant (0)
  B = 1 << 2,   // constant (4)
  C = B | 1,    // constant (5)
  D = "xy".length, // computed
}

Try it

TypeScript Constant and computed members

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 Initializer required after a computed member

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

A member initializer is a constant enum expression when it is one of:

Form Example
Literal 1, "a", -3
Reference to an earlier member of this or another constant enum A, Other.B, Other["B"]
Parenthesized constant expression (1 + 2)
Unary + - ~ on a constant numeric expression ~0
Binary + - * / % << >> >>> & | ^ on constant numeric expressions 1 << 3

Anything else, such as a function call, a property read of a non-enum value or a string method, makes the member computed.

Edge cases: an expression that evaluates to NaN or Infinity is an error in a constant position. A const enum may not contain computed members at all.

Usage notes

  • Constant members get their own literal types (Bits.A is a type). Once an enum contains any computed member, the individual members lose that status and the enum type no longer works as a union of members, so exhaustiveness checks and narrowing are weaker.
  • The member after a computed member needs an explicit initializer, because the compiler cannot know the previous number. A string member never auto-increments either.
  • Names can be string literals ("two words" = 2), but you then read them with bracket syntax.
  • Computed values run once, when the enum object is created, in declaration order. An earlier computed member can be read by a later initializer.
  • Prefer constants for flags. Computed members are best kept for values such as lengths derived from a literal.