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.

A numeric enum in TypeScript is a strongly typed data structure that defines a collection of named constants mapped to discrete, auto-incrementing numeric values. By default, the underlying numeric values are zero-indexed, meaning the first member is assigned 0, and each subsequent member increments by 1.
enum Direction {
  Up,    // 0
  Down,  // 1
  Left,  // 2
  Right  // 3
}

Custom Initialization

You can override the default zero-indexing by explicitly assigning a numeric value to one or more members. Uninitialized members that follow an initialized member will automatically increment by 1 from the preceding value.
enum Status {
  Pending = 1,   // 1
  InProgress,    // 2
  Resolved = 10, // 10
  Rejected       // 11
}

Constant and Computed Members

Numeric enum members fall into two categories: constant and computed.
  • Constant members are evaluated at compile time. They include literal numbers, references to previously defined constant enum members, and simple mathematical or bitwise expressions.
  • Computed members are evaluated at runtime, typically via a function call.
If a numeric enum contains a computed member, the uninitialized member immediately following it will result in a compiler error because the compiler cannot determine the starting value for the increment. An explicit initializer is required to resume the sequence. Once a subsequent member is initialized with a constant, auto-incrementing behaves normally for any uninitialized members that follow it.
const getDynamicValue = () => Math.floor(Math.random() * 10);

enum BitwiseAndComputed {
  None = 0,
  Read = 1 << 0,              // 1 (Constant)
  Write = 1 << 1,             // 2 (Constant)
  ReadWrite = Read | Write,   // 3 (Constant)
  Dynamic = getDynamicValue(),// Computed at runtime
  // InvalidMember,           // Error: Enum member must have initializer.
  Manual = 10,                // Explicitly initialized constant
  AutoIncrement               // 11 (Auto-increments from Manual)
}

Reverse Mapping

Unlike string enums, numeric enums support reverse mapping. The TypeScript compiler generates an object that maps the member name to its numeric value, and the numeric value back to its member name.
enum ResponseCode {
  Success = 200,
  NotFound = 404
}

// Forward mapping (Name -> Value)
const code: number = ResponseCode.Success; // 200

// Reverse mapping (Value -> Name)
const codeName: string = ResponseCode[200]; // "Success"

Compilation Output

To facilitate reverse mapping, TypeScript compiles numeric enums into an Immediately Invoked Function Expression (IIFE) in JavaScript. The assignment ResponseCode["Success"] = 200 returns 200, which is then used as the key for the reverse assignment ResponseCode[200] = "Success".
// Compiled JavaScript output for the ResponseCode enum
var ResponseCode;
(function (ResponseCode) {
    ResponseCode[ResponseCode["Success"] = 200] = "Success";
    ResponseCode[ResponseCode["NotFound"] = 404] = "NotFound";
})(ResponseCode || (ResponseCode = {}));
Master TypeScript with Deep Grasping Methodology!Learn More