Typescript: Unreachable code defaults

Created on 17 Jul 2018  路  4Comments  路  Source: microsoft/TypeScript

Apparently, since 2.9.1, unreachable code is allowed by default, you have to explicitly set allowUnreachableCode to false in tsconfig.json to get the old behavior.

This doesn't match what's described in the docs.

image

Quick repro :
package.json

{
  "name": "tmp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "typescript": "2.9.1"
  }
}

tsconfig.json (default from tsc --init)

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true
  }
}

index.ts

export const a = (): boolean => {
    return true;
    return false;
};

yarn tsc outputs no warning as of 2.9.1

Docs

Most helpful comment

The documentation is still wrong. When the compiler is called in command line mode, unreachable code is not reported at the console unless allowUnreachableCode is explicitly set to false.
see #24261

All 4 comments

See #24320

The docs on --allowUnreachableCode just cost me half an hour; I hope that PR hasn't stalled, for the sake of others.

The documentation is still wrong. When the compiler is called in command line mode, unreachable code is not reported at the console unless allowUnreachableCode is explicitly set to false.
see #24261

A note for Visual Studio (vscode) users: it appears that when vscode analyzes TypeScript code, and allowUnreachableCode is unset in tsconfig.json, it follows the documented behavior and assumes the default is false. This can lead you to a bad time when your command-line compiler and Visual Studio disagree on whether unreachable code is allowed.

Picking a value and setting it in tsconfig.json appears to fix the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fdecampredon picture fdecampredon  路  358Comments

chanon picture chanon  路  138Comments

yortus picture yortus  路  157Comments

OliverJAsh picture OliverJAsh  路  242Comments

rbuckton picture rbuckton  路  139Comments