TypeScript Tutorial: Enums
Named constant sets with numeric and string enums, const enums, and when to use them.
Numeric Enums
```typescript
enum Direction {
North, // 0
South, // 1
East, // 2
West, // 3
}
let dir: Direction = Direction.North;
console.log(dir); // 0
console.log(Direction[0]); // "North" — reverse mapping
```
---
String Enums
More readable than numeric — prefer these in most cases:
```typescript
enum Status {
Pending = "PENDING",
Active = "ACTIVE",
Closed = "CLOSED",
}
function process(status: Status) {
if (status === Status.Active) {
console.log("Processing active record");
}
}
```
---
Const Enums
Completely erased at compile time — just inlines the values. Faster but no reverse mapping:
```typescript
const enum Color {
Red = "RED",
Green = "GREEN",
Blue = "BLUE",
}
const c: Color = Color.Red; // compiles to: const c = "RED"
```
---
Enum vs Union Type
For most cases, a **string literal union** is simpler:
```typescript
// Enum
enum Direction { North, South, East, West }
// Equivalent union — preferred by many TypeScript teams
type Direction = "north" | "south" | "east" | "west";
```
Use enums when you need reverse mapping or want a shared named namespace. Use unions for simple flags and status values.
---
What's Next?
Next: **Utility Types** — TypeScript's built-in type transformers like `Partial`, `Required`, `Pick`, and `Omit`.
What you'll learn in this TypeScript enums tutorial
This interactive TypeScript tutorial has 12 hands-on exercises. Estimated time: 10 minutes.
- String enums — readable constants — String enums assign explicit string values to each member. They appear as readable strings in logs and JSON responses — …
- Numeric enums — automatic values — Numeric enums auto-assign incrementing values starting at 0. They support reverse mapping — you can look up the name fro…
- Const enums — zero-overhead enums — `const enum` is completely erased at compile time — TypeScript replaces every usage with the literal value. This elimina…
- Enums vs union types — when to use each — Many TypeScript teams prefer union types over enums for simplicity. Here's a comparison — when does each approach shine?
- Enums as function parameters — Enums serve as descriptive parameters that prevent magic strings or magic numbers. They make function calls self-documen…
- Enum member expressions — Numeric enum members can be computed using bit flags. This is a classic pattern for representing combinations of options…
- Iterating over enum values — You can iterate over a numeric enum's values, but it requires care because numeric enums have both forward and reverse m…
- Ambient enums — working with external definitions — When declaring types for external JavaScript code (like a third-party library), you use `declare enum` to describe enums…
- Enum in a class — bundling related constants — Enums can be declared inside or alongside classes to bundle related constants. This creates a clean namespace.
- Enum exhaustiveness check — One of the most powerful uses of enums is exhaustiveness checking — TypeScript can verify that every enum value is handl…
- Map with enum keys — type-safe lookup tables — Using an enum as the key type in a `Map` or `Record` creates exhaustive lookup tables where TypeScript ensures every enu…
- Enum values as object keys with computed properties — Enum members can be used as computed property names in object literals. This creates type-safe mappings between enum val…
TypeScript Enums concepts covered
- Numeric Enums
- String Enums
- Const Enums
- Enum vs Union Type
- What's Next?