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.
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
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.
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.
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
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'").
noFallthroughCasesInSwitchreports 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 xin two cases is a redeclaration error unless each case uses its own braces. - A
defaultthat assigns toneverfails with TS2322 when a union member is unhandled.
Usage notes
- Strict equality.
NaNnever matches a case, andcase "1"does not match the number1. - Narrowing by discriminant.
switch (s.kind)narrowss;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 acase, 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; aswitchis better when branches need different narrowing. - Fall-through is a hazard. Enable
noFallthroughCasesInSwitchand comment intentional cases.
Related
- if and else, break and continue, enum, throw
- Lessons: Discriminated unions, Narrowing, Union types