Skip to content

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

@greg-hornby-roam

Description

@greg-hornby-roam

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions