TypeScript 7 · Utility types and narrowing

Check a switch handles every case

Make the compiler fail if a union member is not handled.

function label(status: Status): string {
  switch (status) {
    case "idle": return "Idle";
    case "loading": return "Loading";
    case "error": return "Error";
    default: {
      const unreachable: never = status;
      return unreachable;
    }
  }
}

Official TypeScript 7 documentation

More utility types and narrowing commands

All utility types and narrowing commands · TypeScript 7 cheatsheet

Was this page helpful?