Enum (TypeScript)
An enum is a named set of related constants, defined with the `enum` keyword. It gives friendly names to a fixed group of values, such as directions or statuses.
TypeScript
Plain JavaScript has no enums, so you'd use a set of constants. TypeScript's enum bundles them under one name; by default the members get numeric values starting at 0, but you can assign string values too.
Enums are one of the few TypeScript features that emit real JavaScript code rather than being erased. Many teams prefer a union of string literals instead, but enums remain common and convenient.
Syntax
enum Name { A, B, C }Examples
TypeScript
enum Direction {
Up,
Down,
}
console.log(Direction.Up); // 0
console.log(Direction.Down); // 1Output:
0
1TypeScript
enum Status {
Active = "ACTIVE",
Done = "DONE",
}
console.log(Status.Active);Output:
ACTIVE