Course contentsShow
← TypeScript
Lesson 414 of 1,7969. Union TypesFree lesson

Discriminated Unions vs Enums

Why discriminated unions often replace enums for more flexible and type-safe variants.

Discriminated Unions vs Enums

What you'll learn: Why discriminated unions often replace enums for more flexible, type-safe variants that feel more "TypeScript native."

The Core Difference

Both enums and discriminated unions help represent a fixed set of options, but they work differently. Enums create actual JavaScript objects at runtime (in most cases), while discriminated unions are pure type-level constructs that compile away completely. Think of enums as pre-built furniture from a store, while discriminated unions are custom-built pieces you assemble with types you already know.

Why Discriminated Unions Often Win

Discriminated unions leverage the union narrowing and exhaustiveness checking you've already mastered. They're just string literals (or other primitives) plus objects—no special syntax to learn.

When you need more than simple values, discriminated unions shine. They let you attach different data to different variants naturally, something enums struggle with. Each variant can have its own shape, and TypeScript's narrowing works automatically.

They also integrate seamlessly with the rest of your type system. No numeric/string duality confusion. No reverse mappings to trip over. Just straightforward union types behaving exactly as you'd expect from everything you've learned about unions.

When Enums Still Make Sense

Enums can be useful when you truly need a runtime object—perhaps for iteration or reverse lookups. They also feel familiar to developers from languages like C# or Java. But in modern TypeScript codebases, you'll increasingly see discriminated unions preferred for their simplicity and predictability.

Key Takeaway

Discriminated unions are typically preferred over enums because they're type-only constructs that leverage TypeScript's native union narrowing, offer more flexibility for variant-specific data, and avoid runtime overhead and special-case behaviors.