Skip to content

Incorrect hover shown for union variable in while loop following an exhaustive switch #47538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
greg-hornby-roam opened this issue Jan 21, 2022 · 3 comments
Labels
Bug A bug in TypeScript Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Help Wanted You can do this
Milestone

Comments

@greg-hornby-roam
Copy link

Bug Report

πŸ”Ž Search Terms

while switch union narrowing never

πŸ•— Version & Regression Information

Version: 4.5.4

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about while, loop, switch

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

let foo: "aaa" | "bbb" = "aaa";

while (true) {
    switch (foo) {
        case "aaa":
            1+1; //any statement
            break;
        case "bbb":
            1+1; //any statement
            break;
    }

    if (foo === "aaa") {
        console.log("Foo is aaa");
        foo = "bbb";
    } else if (foo === "bbb") { //says foo is never
        console.log("Foo is bbb");
        console.log("Loop is done");
        break;
    }
}

πŸ™ Actual behavior

On the line } else if (foo === "bbb") { when you hover over foo typescript thinks the type is never. If that was the case then you'd expect nothing inside that block to run.

πŸ™‚ Expected behavior

foo should be type "bbb" here, and if you remove the switch statement it is. Furthermore if you run the compiled JS, it does hit all the console.log lines and produces

Foo is aaa
Foo is bbb
Loop is done
@RyanCavanaugh
Copy link
Member

This looks like a quickinfo bug only; the correct things happen (including if we remove the should-be redundant if (foo === "bbb") part)

    } else if (foo === "bbb") {
        let b: "bbb" = foo; // ok
        let a: "aaa" = foo; // errors as expected

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Help Wanted You can do this labels Jan 21, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jan 21, 2022
@RyanCavanaugh RyanCavanaugh changed the title Switch inside while loop breaks union narrowing Incorrect hover shown for union variable in while loop following an exhaustive switch Jan 21, 2022
@RyanCavanaugh
Copy link
Member

Playing with this sample showed an additional bug, though: #47539

@MQuy
Copy link
Contributor

MQuy commented Mar 17, 2022

hi, I would like to work on this bug :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants