Course contentsShow
← TypeScript
Lesson 206 of 1,7965. Type Narrowing FundamentalsFree lesson

Switch Statement Exhaustiveness

How TypeScript narrows types through switch cases and ensures all cases are handled with default never.

Switch Statement Exhaustiveness

What you'll learn: How TypeScript uses switch statements to narrow types and ensure you've handled every possible case.


When you have a union type and use a switch statement to handle each variant, TypeScript tracks which cases you've covered. This is called exhaustiveness checking — making sure you haven't forgotten any possibilities.

How Switch Narrowing Works

Inside each case block, TypeScript narrows the type to that specific literal:

type Status = 'pending' | 'approved' | 'rejected';

function handleStatus(status: Status) {
  switch (status) {
    case 'pending':
      // status is narrowed to 'pending' here
      console.log('Waiting...');
      break;
    case 'approved':
      // status is narrowed to 'approved' here
      console.log('Success!');
      break;
    case 'rejected':
      // status is narrowed to 'rejected' here
      console.log('Failed');
      break;
  }
}

The default: never Pattern

To catch missing cases at compile time, add a default clause that assigns the remaining value to a never type:

function handleStatus(status: Status) {
  switch (status) {
    case 'pending':
      console.log('Waiting...');
      break;
    case 'approved':
      console.log('Success!');
      break;
    // Oops! Forgot 'rejected'
    default:
      const exhaustive: never = status; // Error!
      // Type 'rejected' is not assignable to 'never'
  }
}

If you've handled all cases, the default block is unreachable, and status narrows to never (which is valid). If you miss a case, TypeScript complains because the leftover type can't be assigned to never.

Why This Matters

Think of it like a checklist: TypeScript verifies you've ticked every box. If you add a new status later, the compiler forces you to handle it everywhere.

Key Takeaway: Use switch statements with a default: never clause to let TypeScript guarantee you've handled every possible value in a union type.