Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript noFallthroughCasesInSwitch option

Reference for noFallthroughCasesInSwitch: error TS7029 when a non-empty case runs into the next one, and how break, return, throw and stacked labels avoid it.

In a switch, a case that does not end with break, return, throw or continue keeps running into the next case. Sometimes that is intended, but a forgotten break is one of the oldest bugs in C-style languages.

noFallthroughCasesInSwitch reports an error for any non-empty case clause that can fall through to the next one. Several labels stacked together with no statements between them are still fine. The option is not part of strict.

TypeScript
switch (code) {
  case 1:
    log("one"); // falls into case 2 -> error with the option on
  case 2:
    log("two");
    break;
}

Try it

TypeScript Fallthrough in action

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 Patterns the option allows

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

JSON
{ "compilerOptions": { "noFallthroughCasesInSwitch": true } }
Part Description
Value true or false. Default: false. Not included in strict.
Error TS7029: Fallthrough case in switch. reported on the case clause that falls through.
Not reported Empty clauses stacked on one body, the last clause, and clauses ending in break, return, throw or continue.

Usage notes

  • Intentional fallthrough must be rewritten: share code through a helper function, stack the labels, or restructure the logic. There is no comment directive that silences it other than // @ts-ignore on that line.
  • The last clause (usually default) cannot fall through to anything, so it is never reported.
  • default in the middle counts like any other clause.
  • Exhaustive unions. A switch over a discriminated union is a good use of the option: every case returns, and a forgotten return becomes visible.
  • Linters. ESLint's no-fallthrough is similar; the compiler flag is enough for most projects.
  • Reachability analysis follows control flow, so a clause ending in an if that returns on every branch is accepted.