Course contentsShow
← TypeScript
Lesson 963 of 1,79622. The never TypeFree lesson

When to Skip Exhaustiveness Checks

Situations where explicit exhaustiveness adds noise and when default behavior is acceptable.

When to Skip Exhaustiveness Checks

What you'll learn: When explicit exhaustiveness checks add unnecessary noise and when relying on default behavior is perfectly acceptable.

The Exhaustiveness Tax

You've learned how never and assertNever help catch missing union cases at compile time. But exhaustiveness checking isn't free—it adds code, complexity, and maintenance overhead. Sometimes that cost exceeds the benefit.

When Exhaustiveness Checks Make Sense

Use exhaustive checks when:

  • The union changes frequently (adding features, states, or types)
  • Missing a case causes silent failures (no runtime error, just wrong behavior)
  • The code is in a critical path (payment processing, security logic)
  • Multiple developers modify the code (team safety net)

When to Skip Them

Skip exhaustiveness checks when:

1. Default Behavior Is Intentional

If you genuinely want a fallback case, exhaustiveness adds noise:

type LogLevel = 'error' | 'warn' | 'info' | 'debug';

function formatLog(level: LogLevel): string {
  switch (level) {
    case 'error': return '❌';
    case 'warn': return '⚠️';
    default: return 'ℹ️'; // Intentional catch-all
  }
}

No need for assertNever here—you want a default.

2. The Union Is Stable

If a type rarely changes (e.g., built-in types like true | false), exhaustiveness checks feel like overkill:

function boolToString(val: boolean): string {
  return val ? 'yes' : 'no'; // No need to check every case
}

3. Partial Handling Is Acceptable

Sometimes you only care about specific cases and want to ignore others gracefully:

type Event = { type: 'click' } | { type: 'scroll' } | { type: 'resize' };

function trackImportantEvents(event: Event): void {
  if (event.type === 'click') {
    // Track click
  }
  // Ignore other events—no exhaustiveness needed
}

4. Prototype or Throwaway Code

Early development stages don't always justify the rigor.

The Readability Balance

Think of exhaustiveness checks like input validation: essential at boundaries, excessive internally. If adding a union member would obviously break nearby tests or cause loud runtime failures, the compiler check may be redundant.

Key Takeaway: Use exhaustiveness checks when union changes are likely and silent failures are dangerous. Skip them when you have intentional defaults, stable types, or partial handling—trust your judgment on where the safety-to-noise ratio tips.