Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript switch statement

Reference for switch in TypeScript: strict equality matching, fall-through, narrowing discriminated unions, exhaustiveness checks with never and noFallthroughCasesInSwitch.

switch compares one value against several case labels using strict equality (===) and runs the matching branch. Without break, return or throw, execution falls through into the next case.

In TypeScript, switch is the natural partner of union types. Inside each case, the switched expression is narrowed to the matching member, and a default branch that assigns the value to never proves that every member is handled.

TypeScript
type Light = "red" | "yellow" | "green";
function next(l: Light): Light {
  switch (l) {
    case "red": return "green";
    case "green": return "yellow";
    case "yellow": return "red";
  }
}

Try it

TypeScript Discriminated union with exhaustive check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript A forgotten case is caught

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Fall-through and grouped cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
switch (expression) {
  case value1:
    statements;
    break;
  case value2:
  case value3:
    statements;
    break;
  default:
    statements;
}
Part Description
expression Evaluated once.
case value: Compared with ===. Any expression is allowed, not just constants.
default: Runs if nothing matched; may appear anywhere in the block.
break Leaves the switch.

Errors and edge cases:

  • A case whose value cannot equal the switched type is error TS2678 ("Type '"x"' is not comparable to type 'number'").
  • noFallthroughCasesInSwitch reports a non-empty case that falls through (TS7029, "Fallthrough case in switch"). Empty cases that group labels are fine.
  • All cases share one block scope. Declaring let x in two cases is a redeclaration error unless each case uses its own braces.
  • A default that assigns to never fails with TS2322 when a union member is unhandled.

Usage notes

  • Strict equality. NaN never matches a case, and case "1" does not match the number 1.
  • Narrowing by discriminant. switch (s.kind) narrows s; switch (typeof x) narrows by primitive type; switch (true) with boolean cases also narrows in modern TypeScript.
  • Exhaustiveness without default. If every union member has a case, TypeScript knows the function returns on all paths, so a missing return type error disappears.
  • Enums. A switch over a union enum is exhaustiveness-checked the same way.
  • Prefer a lookup object (Record<Kind, Handler>) when branches are tiny and uniform; a switch is better when branches need different narrowing.
  • Fall-through is a hazard. Enable noFallthroughCasesInSwitch and comment intentional cases.