Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An enum member is an individual named constant defined within a TypeScript enumeration (enum). Each member is bound to a numeric or string value and serves as both a distinct value at runtime and a unique literal type at compile time.
enum EnumName {
  MemberName1,             // Implicitly initialized
  MemberName2 = "Value",   // Explicitly initialized
  MemberName3 = 1 << 2     // Constant expression
}
TypeScript strictly categorizes enum members into two classifications based on how their values are resolved: constant members and computed members.

Constant Members

A member is constant if its value can be fully resolved at compile time. TypeScript considers an enum member constant under the following conditions:
  1. Implicit Initialization: It is the first member in the enum and has no initializer (defaults to 0).
  2. Auto-incrementation: It has no initializer, and the preceding member was a numeric constant. The member’s value is the preceding member’s value plus 1.
  3. Constant Enum Expression: It is initialized using a subset of TypeScript expressions evaluated during compilation. This includes string literals, numeric literals, parenthesized constant expressions, and bitwise/arithmetic operators applied to other constant members.
enum ConstantMembers {
  None,               // Constant: implicitly 0
  Read = 1 << 1,      // Constant: bitwise operation (2)
  Write = 1 << 2,     // Constant: bitwise operation (4)
  ReadWrite = Read | Write // Constant: references other constant members (6)
}

Computed Members

A member is computed if its initializer is an expression that must be evaluated at runtime. This typically involves function calls, property accessors, or variables outside the enum’s scope.
function getBaseValue(): number { return 10; }

enum ComputedMembers {
  A = getBaseValue(),      // Computed: resolved at runtime
  B = Math.random(),       // Computed: resolved at runtime
  C = 1,                   // Explicitly initialized numeric constant
  D                        // Constant: auto-incremented to 2
}
Note: If an enum member is computed (or initialized with a string), the member immediately following it must be explicitly initialized. However, if a subsequent member is explicitly initialized with a numeric constant, auto-incrementation safely resumes for the members after it.

Value Types and Initialization Rules

Enum members are further defined by the data type of their initialized value:
  • Numeric Members: Support implicit initialization and auto-incrementation. They map bidirectionally in the compiled JavaScript (name-to-value and value-to-name).
  • String Members: Do not support auto-incrementation. Every string member must be explicitly initialized with a string literal or another string enum member. They only map unidirectionally (name-to-value).
  • Heterogeneous Members: TypeScript permits mixing numeric and string members within the same enum declaration, provided each member adheres to its respective initialization rules.
enum HeterogeneousMembers {
  No = 0,             // Numeric member
  Yes = "YES",        // String member
}

Member Types (Literal Types)

As of TypeScript 5.0, the enum architecture treats all enums as union enums. Consequently, every enum member acts as its own distinct literal type, regardless of whether the enum contains computed or constant members. This allows the type system to strictly enforce that a variable can only accept a specific member’s value, rather than any value from the broader enum.
enum Status {
  Active = "ACTIVE",
  Inactive = "INACTIVE",
  Pending = Math.random() // Computed member
}

// Status.Active is used as a distinct type, not just a value
let currentStatus: Status.Active;

currentStatus = Status.Active;   // Valid
currentStatus = Status.Inactive; // Type Error: Type 'Status.Inactive' is not assignable to type 'Status.Active'
Master TypeScript with Deep Grasping Methodology!Learn More