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.
function f(): number { return 1; console.log("never runs"); // TS7027 when allowUnreachableCode is false }
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.
Syntax
{ "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,breakorcontinue; code after an infinite loop with nobreak; code after a call to a function whose declared return type isnever(the call must be to a function with an explicitneverannotation). - Declarations are exempt. Function and
vardeclarations, 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
returnwhile debugging; the default mode keeps the build green and still dims the dead code. - CI strictness. Use
falsefor projects that treat dead code as a defect. - Narrowing to
never. A branch where a value has narrowed toneveris not reported unreachable; use an explicitneverassertion function if you want that.