Skip to content
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

Improve type narrowing on switch(true) statements #46600

Closed
ElianCordoba opened this issue Oct 30, 2021 · 1 comment
Closed

Improve type narrowing on switch(true) statements #46600

ElianCordoba opened this issue Oct 30, 2021 · 1 comment

Comments

@ElianCordoba
Copy link

Bug Report

🔎 Search Terms

  • switch narrowing
  • switch(true)

🔗 Related issues

Maybe these ones too

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type guards

⏯ Playground Link

Playground link with relevant code

💻 Code

const char = "(" as string;

// This works fine, char inside the if statements get's property narrowed to type "("
if (char === '(') {
  char;
}

// This doesn't work, char inside the case statement is of type string
switch (true) {
  case char === '(':
    char;
}

🙁 Actual behavior

Char inside the case statement in a switch(true) has a type string

🙂 Expected behavior

It should have type "("

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 30, 2021

Duplicate of #37178. Used search terms: switch true in:title

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

2 participants