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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With