Skip to content

If statement variable reassignment fail type check #43663

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

Closed
dabreadman opened this issue Apr 13, 2021 · 3 comments
Closed

If statement variable reassignment fail type check #43663

dabreadman opened this issue Apr 13, 2021 · 3 comments

Comments

@dabreadman
Copy link

Bug Report

🔎 Search Terms

  • if statement variable reassignment not assignable to parameter of type
  • if statement variable reassignment not assignable
  • if statement variable reassignment
  • variable reassignment type
  • variable reassignment

🕗 Version & Regression Information

  • Relatively new to Typescript, encountered this on typescript@4.2.4

⏯ Playground Link

Playground link with relevant code

💻 Code

function fooBar(foo: "bar" | "baz"): boolean {
  return foo === "bar";
}

function fishTank(): boolean {
  let input = "";
  if (!input) {
    input = "bar";
  } else if (input !== "bar" && input !== "baz") {
    return false;
  }
  return fooBar(input);
}

🙁 Actual behavior

Failed to compile, Argument of type 'string' is not assignable to parameter of type '"bar" | "baz"'.ts(2345)

🙂 Expected behavior

Compiles successfully.

Flow

@DanielRosenwasser
Copy link
Member

I believe we just don't narrow infinite types to singleton types in equality checks. @andrewbranch was dealing with a similar issue, but I can't recall what the reasoning was.

@andrewbranch
Copy link
Member

@dabreadman
Copy link
Author

I see, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants