Skip to content

Control Flow Analysis of Aliased Conditions and Discriminants doesn't work with === true #49589

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
andrenanninga opened this issue Jun 17, 2022 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@andrenanninga
Copy link

andrenanninga commented Jun 17, 2022

Bug Report

🔎 Search Terms

  • Control flow
  • Type narrowing

🕗 Version & Regression Information

This is behavior that exists in every version, expected to work since v4.4 when "Control Flow Analysis of Aliased Conditions and Discriminants" was introduced.

⏯ Playground Link

Playground link with relevant code

💻 Code

function foo(arg: unknown) {
  const argIsString = typeof arg === "string";
  
  if (argIsString) {
    console.log(arg.toUpperCase());
    // No error, works as expected
  }

  if (argIsString === true) {
    console.log(arg.toUpperCase());
    //          ~~~
    // Error! Object is of type 'unknown'.
  }
}

🙁 Actual behavior

Using if (argIsString) correctly narrows arg to type string. Using if (argIsString === true) however does not narrow the arg parameter to string.

🙂 Expected behavior

I expect if (argIsString) & if (argIsString === true) to have the same behavior in terms of type narrowing in this case.

@MartinJohns
Copy link
Contributor

Duplicate of #44366. Last duplicate was created not even a day ago: #49573

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants