Unintuitive error message with 'unreachable' code
See original GitHub issueTypeScript Version: 2.0.02
Code
const enum Keys {
Tab = 10,
Shift = 13
}
function bug() {
let key: Keys;
if (key === Keys.Tab) {
return;
}
if (key === Keys.Tab || key === Keys.Shift) {
console.log('Bug');
}
}
On line 13 you get an error message that === can’t be applied as an operator. Took me quite some time to find out that the same comparison was above (the real code was a little bit more complicated) and was leaving the function.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Unreachable code: error or warning? [closed] - Stack Overflow
An error means that the compiler is physically unable to deal with your code. A warning means that the compiler is capable of...
Read more >Unreachable Code Error in Java - GeeksforGeeks
The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable ...
Read more >DownloadPipelineArtifact@2 unreachable code reached
Regarding of the error message, I will report the ticket to product group for further investigation. Thanks for helping us build a better...
Read more >Unreachable code for common django exception · Issue #2770
Python 3.8, Ubuntu 20.04, VScode 1.63.2 I'm getting unreachable code for the second block in this piece of code: try: content_type ...
Read more >Exception handling unreachable code after try-catch block
Simply, in order for the compiler to determine that the code is unreachable, it must check every possible path. In your first example,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem is that there’s no
never
type in play here.If we wanted to get really fancy, when this kind of check fails, we could recompute the relation using the original declared types instead of the narrowed types. If that relation succeeded, then the error message could say something like
+1 for @RyanCavanaugh proposal. Although I would leave the ‘Do you have a logic error’ out.