An enum (short for enumeration) is a special data type that lets you define a set of named constants.

enum Direction {
  North, // 0
  East, // 1
  South, // 2
  West, // 3
}

const move = Direction.North; // move = 0

Custom Values

You can assign custom values to the enum values. The members that follow keep counting up from there.

enum Direction {
  North = 2, // 2
  East, // 3
  South, // 4
  West, // 5
}

const move = Direction.South; // move = 4

String Enum

You can even assign string values in enum.

enum LogLevel {
  Info = "INFO",
  Warning = "WARNING",
  Error = "ERROR",
}

const log = LogLevel.Error; // log = "ERROR"

// The following line will throw an error for string enum, this works only with numeric enum.
console.log(LogLevel[0]);

Enums Exist at Runtime

Unlike most TypeScript features, an enum is not erased: tsc turns it into a real JavaScript object. That's why a numeric enum can be looked up in both directions:

enum Direction {
  North,
  East,
}

Direction.North; // 0
Direction[0]; // "North" (reverse mapping, numeric enums only)
Because enums generate code, tools that only strip types (such as running .ts files directly in Node.js) don't support them. A union of string literals ("north" | "east") is a common alternative. See Union & Literal Types.