-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Type of variable isn't widened inside a conditional inside a loop #46475
Comments
No, there are actually two variables: |
Clarification on above comment: I was responding to someone who mistakenly thought |
A little more reduced: https://www.typescriptlang.org/play?ts=4.6.0-dev.20211105#code/FAGwpgLgBAxg9gEzALigBigHygRi1AJigF51hgAzOAJygAoBuBgSigG9govZEwSekJYqSIB+XFFRpy3KAEsK9AIRKKAQxABnMKw6zZ8QSM7cAvsHNA The code in the screenshot is kind of easier to follow even though it’s longer since it’s real. |
Question: I've just encountered this which looks similar to me, the root problem in both cases being that TypeScript is very quick to narrow types on Would you say this is the same issue or that I should raise a seperate bug? |
@jameswilddev it's duplicate of #44824 |
@RyanCavanaugh Probably want to update the milestone again. :-) |
@ahejlsberg I started investigating this bug a while back but couldn't come up with a fix, so it was reassigned to you, but now I'm wondering if I could fix this along with a fix for #47539. Are you planning on working on this? |
The issue here is that during control flow analysis of the loop, the type of the |
Bug Report
🔎 Search Terms
ts2367 widen loop
🕗 Version & Regression Information
⏯ Playground Link
Playground link
💻 Code
Edit: shorter version by gabritto
And in practice:
🙁 Actual behavior
Error
🙂 Expected behavior
Since
code
could be changed in previous iterations of the loop, the condition is not always false, so there shouldn't be any error.The text was updated successfully, but these errors were encountered: