Skip to content

Discriminated union exhaustiveness check fails

Quick summary

Your switch statement is missing a case for a union member — the exhaustiveness check via 'default: assertNever' catches it.

Why this happens

  • A new variant was added to the discriminated union but the switch was not updated

  • The discriminant property name* is misspelled in one of the cases, so it does not match

  • The exhaustiveness check function (assertNever) is missing or not called in the default case

Minimal example

✗ Broken code
type Action = { type: 'start' } | { type: 'stop' } | { type: 'pause' };
function handle(action: Action) {
  switch (action.type) {
    case 'start': return 1;
    case 'stop': return 2;
    // missing 'pause' case!
  }
}
✓ Fixed code
type Action = { type: 'start' } | { type: 'stop' } | { type: 'pause' };
function assertNever(x: never): never { throw new Error(); }
function handle(action: Action) {
  switch (action.type) {
    case 'start': return 1;
    case 'stop': return 2;
    case 'pause': return 3;
    default: return assertNever(action); // compile error if missing case
  }
}

The assertNever function in the default case makes TypeScript error at compile time if any union variant is not handled.

How to diagnose

  • Check the union type definition for all variants and compare with the switch cases

  • Use the assertNever function in the default case to get a compile error for missing cases

  • Hover over the discriminant in the switch to see its type and all possible values

How to fix

  • Add the missing case for the new union variant

  • Fix the discriminant property name spelling in the case clause

  • Add an assertNever(x: never) function call in the default case to enforce exhaustiveness at compile time

How to prevent

  • Always use an assertNever exhaustiveness check in the default case of discriminated union switches

  • When adding a new union variant, the compiler will error on all non-exhaustive switches

Related Resources

Related Glossary

Related Lessons

← Back to language