Skip to content

If statement variable reassignment fail type check #43663

Closed
@dabreadman

Description

@dabreadman

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions