Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make "Unreachable code detected" compilation warning instead of error in TypeScript?

Tags:

typescript

In Typescript, when there is unreachable code, the compiler generates by default error:

error TS7027: Unreachable code detected.

I can avoid it by using compiler option allowUnreachableCode to true but can I change it somehow to warning?

I would still prefer to know about unreachable code. During development and debugging I sometimes use if (false) to temporarily remove some functionality, which is better than commenting the code out especially when the code itself contains comments.

In C#, unreachable code generates warning (which I can treat as error by other option), which seems to me better way to handle it.

like image 908
eXavier Avatar asked Jun 07 '17 08:06

eXavier


People also ask

What is unreachable code detected?

In computer programming, unreachable code is part of the source code of a program which can never be executed because there exists no control flow path to the code from the rest of the program.

What is unreachable code Vscode?

Unreachable code, a part of the source code that will never be executed due to inappropriate exit points/control flow. The other kind of unreachable code is referred as dead code, although dead code might get executed but has no effect on the functionality of the system.

What does unreachable code mean in C?

MISRA C defines unreachable code as code that cannot be executed, and it defines dead code as code that can be executed but has no effect on the functional behavior of the program.


1 Answers

In the particular case of temporarily removing functionality (which I think is the main case where one would want to have unreachable code), writing

if( <any>false ) {
  // unused code goes here
}

will prevent the TypeScript compiler from complaining, and give the runtime ECMAScript implementation a pretty good chance of optimizing the code away.

Alternatively,

if( 0 ) {
  // unused code goes here
}

will be accepted by the TypeScript language, but will still trigger an "unexpected constant condition" warning from tslint.

like image 156
hmakholm left over Monica Avatar answered Sep 23 '22 12:09

hmakholm left over Monica