Skip to content
TypeScript

Enum

Numeric, string, and const enums.

By EZ4Code Team
enumenum

Code

enum Direction {
  Up = 1,
  Down,
  Left,
  Right
}

enum Color {
  Red = "RED",
  Green = "GREEN",
  Blue = "BLUE"
}

const enum Status {
  Active,
  Inactive
}

const s = Status.Active; // Inlined at compile time

// Reverse mapping (numeric enums only)
console.log(Direction[1]); // "Up"
console.log(Direction.Up);  // 1

Explanation

Enums define named constants; const enum is inlined at compile time for performance.

More TypeScript Snippets