Syntax
enum Name { Member1, Member2 }Examples
Numeric and String Enums
The two main enum flavors.
enum Direction {
Up, // 0
Down, // 1
Left, // 2
Right // 3
}
enum Status {
Pending = "PENDING",
Active = "ACTIVE",
Completed = "COMPLETED"
}
function move(direction: Direction) {
console.log(Direction[direction]); // reverse lookup: "Up", "Down", etc for numeric enums
}
move(Direction.Up);
console.log(Status.Active); // "ACTIVE"const enum - Zero Runtime Cost
Fully erased at compile time, unlike a regular enum.
const enum Size {
Small,
Medium,
Large
}
let mySize = Size.Medium;
// Compiles down to: let mySize = 1;
// No Size object exists at runtime at all - completely inlinedBest practices
- Consider a literal type union ("small" | "medium" | "large") instead of an enum for simple cases - it requires no import and integrates more naturally with plain JS
- Use string enums over numeric enums when the value might be logged, serialized, or debugged, since string values are self-explanatory
- Use const enum only when you specifically want zero runtime footprint and do not need reverse lookups - be aware it has some restrictions in certain build tools
- Avoid mixing numeric and string values within the same enum, as it can produce confusing reverse-lookup behavior
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts ยท .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.Union and Intersection Types
A union type (A | B) means a value can be either type A or type B. An intersection type (A & B) means a value must satisfy both A and B simultaneously, combining their members into one type. Unions are common for values with a few valid states; intersections are common for combining smaller, focused types into a larger one.Literal Types
Literal types narrow a type down to one specific, exact value rather than a general category - "success" instead of string, or 200 instead of number. They are most useful combined with union types, letting you precisely constrain a value to a specific set of allowed options, catching typos and invalid values at compile time.