Enums & Tuples

An enum gives a fixed set of named values, useful for anything that only ever has a handful of valid states. A tuple is an array with a fixed length and a specific type for each position, rather than one type repeated throughout.

Numeric and string enums

TS status.ts
enum OrderStatus {
  Pending,
  Shipped,
  Delivered,
}

enum Direction {
  Up = "UP",
  Down = "DOWN",
}

const status: OrderStatus = OrderStatus.Shipped;
console.log(status);
console.log(OrderStatus[status]);
console.log(Direction.Up);
Output
1
Shipped
UP

A numeric enum like OrderStatus assigns 0, 1, 2 to its members automatically in order — Shipped is 1. A string enum like Direction instead uses the exact string you write, which tends to be more readable when inspecting values, like in logs.

Tuples

TS tuple.ts
let point: [number, number] = [10, 20];
let entry: [string, number] = ["apples", 5];

const [label, count] = entry;
console.log(`${label}: ${count}`);

point = [10, 20, 30];
tsc output
tuple.ts:6:1 - error TS2322: Type '[number, number, number]'
is not assignable to type '[number, number]'.
  Source has 3 element(s) but target allows only 2.

[string, number] requires exactly two elements, a string first and a number second — order and count both matter, unlike a regular array type where every element shares one type.

Note: a numeric enum's compiled JavaScript includes a reverse mapping — OrderStatus[1] gives back the string "Shipped", as seen above — but a string enum does not generate that reverse lookup at all, since the values aren't sequential numbers to map back from. Also worth knowing: a tuple's fixed length is a compile-time-only guarantee. At runtime a tuple is just a regular JavaScript array, and nothing stops code from calling .push() on it and quietly making it longer than its declared type says it should be.