TypeScript Records With Enum Keys Are Exhaustively Checked

In TypeScript, a Record type used with an enum for keys is exhaustively checked. That means if you forget an enum variant in the Record it’s a compiler error.

enum Status {
  Todo,
  InProgress,
  Done,
}

const StatusIcon: Record<Status, string> = {
  [Status.Todo]: "todo.svg",
  [Status.InProgress]: "in-progress.svg",
  [Status.Done]: "done.svg",
}

See also: