Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript allowUnreachableCode option

Reference for allowUnreachableCode: the three modes undefined, true and false, error TS7027, what counts as unreachable and how never-returning calls affect it.

Code that can never run, such as statements after a return or throw, is almost always a mistake. allowUnreachableCode decides how the compiler treats it, and it has three settings rather than two.

When the option is not set (the default), unreachable code is shown as a faded-out editor hint but is not a compile error. Set it to false and unreachable code becomes error TS7027. Set it to true and the compiler stays silent, with no hint either.

TypeScript
function f(): number {
  return 1;
  console.log("never runs"); // TS7027 when allowUnreachableCode is false
}

Try it

TypeScript Code the compiler can prove is dead

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 Never-returning calls

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": { "allowUnreachableCode": false } }
Value Effect
not set Unreachable code is not an error; editors show it dimmed as a suggestion.
true Unreachable code is ignored completely.
false Unreachable code is an error: TS7027: Unreachable code detected.

Not part of strict; the default is "not set". Added in TypeScript 1.8.

Usage notes

  • What counts as unreachable: statements after return, throw, break or continue; code after an infinite loop with no break; code after a call to a function whose declared return type is never (the call must be to a function with an explicit never annotation).
  • Declarations are exempt. Function and var declarations, and type declarations, are hoisted or have no runtime effect and are not reported.
  • Control flow is syntactic. The compiler does not evaluate conditions: if (false) { ... } is not reported as unreachable.
  • Temporary debugging. Developers sometimes add an early return while debugging; the default mode keeps the build green and still dims the dead code.
  • CI strictness. Use false for projects that treat dead code as a defect.
  • Narrowing to never. A branch where a value has narrowed to never is not reported unreachable; use an explicit never assertion function if you want that.