Skip to main content

TypeScript Discriminated Union Builder

Define union variants visually. Get type-safe TypeScript with switch statements, type guards, and exhaustive checking.

✓ Visual variant builder ✓ Type guards ✓ Exhaustive switch ✓ Redux action patterns

Union Configuration

Presets

Variants

 

Frequently Asked Questions

What is a discriminated union in TypeScript?▼

A discriminated union (also called a tagged union or algebraic data type) is a union type where each member has a common literal property called the discriminant. TypeScript uses this discriminant field to narrow the type in switch/if statements, giving you exhaustive type checking. They model finite sets of states cleanly — like UI states (loading/success/error) or action types in reducers.

When should I use discriminated unions vs optional properties?▼

Use discriminated unions when your data has mutually exclusive states — never allow a combination that makes no sense in your domain. For example, don't use { data?: T, error?: Error, loading?: boolean } — use a union with discriminant 'status': 'loading' | 'success' | 'error'. This prevents impossible states at the type level and forces exhaustive handling.

What is exhaustive checking with never?▼

By adding a default case that assigns the discriminant to 'never', TypeScript will emit a compile error if you forget to handle a union member. Pattern: default: const _exhaustive: never = state; throw new Error('Unhandled state'). When you add a new state to the union, TypeScript will tell you exactly which switch statements need updating.

How do discriminated unions work with switch statements?▼

TypeScript narrows the type inside each case block based on the discriminant value. If the discriminant is 'status' and the case is 'success', TypeScript knows the full shape including success-only fields. This means you get autocomplete, null-checking, and type errors when accessing fields that don't exist in that state — without any runtime overhead.

What is the difference between discriminated unions and class inheritance?▼

Discriminated unions are pure data structures — plain objects with a type discriminant. They're serializable, immutable-friendly, and work great with JSON. Class inheritance requires instantiation and is harder to serialize. In functional TypeScript (Redux, Zustand, React reducers), discriminated unions are preferred. For OOP patterns with methods and polymorphism, classes are more appropriate.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →