question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unintuitive error message with 'unreachable' code

See original GitHub issue

TypeScript 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:open
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
RyanCavanaughcommented, Sep 20, 2016

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

Type `X` is not assignable type `Y`
  Earlier code in this block changed the type of the expressions
  in a way that makes this operation invalid. Do you have a logic error?
0reactions
dbaeumercommented, Sep 21, 2016

+1 for @RyanCavanaugh proposal. Although I would leave the ‘Do you have a logic error’ out.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found