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

Type of variable isn't widened inside a conditional inside a loop #46475

Closed
icecream17 opened this issue Oct 22, 2021 · 9 comments · Fixed by #54380
Closed

Type of variable isn't widened inside a conditional inside a loop #46475

icecream17 opened this issue Oct 22, 2021 · 9 comments · Fixed by #54380
Assignees
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone

Comments

@icecream17
Copy link

icecream17 commented Oct 22, 2021

Bug Report

🔎 Search Terms

ts2367 widen loop

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link

💻 Code

Edit: shorter version by gabritto

let code: 0 | 1 = 0
while (true) {
    code = code === 1 ? 0 : 1
}

And in practice:
image

🙁 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.

@icecream17 icecream17 changed the title Type of variable isn't widened inside an if-else statement inside a loop Type of variable isn't widened inside a conditional inside a loop Oct 22, 2021
@fatcerberus
Copy link

fatcerberus commented Oct 22, 2021

No, there are actually two variables: code and code2. The latter is a loop variable.

@fatcerberus
Copy link

Clarification on above comment: I was responding to someone who mistakenly thought code and code2 in the repro code were the same variable. That comment has since been deleted, but I left my response in case someone else makes the same mistake.

@andrewbranch
Copy link
Member

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.

@andrewbranch andrewbranch added Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis labels Nov 12, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.6.0 milestone Nov 12, 2021
@jameswilddev
Copy link

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 let statements when it should probably leave them as the type they were declared as:

https://www.typescriptlang.org/play?#code/DYUwLgBGAWCWB2BzAXBABgQTRAPugQtnmgMLYC86WA3ALABQDAxgPbwDOk74ElAFGBapMRAqNJoAlKgBuLWABNeAPggBvBhC1Q4SXlBZ16AXyMNuYPhMln6sAGYQBuxL3KVr6zdtYcWoADpgFkQ+AHJEFjCbBmMGBiA

Would you say this is the same issue or that I should raise a seperate bug?

@Zzzen
Copy link
Contributor

Zzzen commented Dec 12, 2021

@jameswilddev it's duplicate of #44824

@MartinJohns
Copy link
Contributor

@RyanCavanaugh Probably want to update the milestone again. :-)

@RyanCavanaugh RyanCavanaugh added the Rescheduled This issue was previously scheduled to an earlier milestone label May 13, 2022
@gabritto
Copy link
Member

gabritto commented Oct 6, 2022

@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?

@ahejlsberg
Copy link
Member

ahejlsberg commented May 24, 2023

The issue here is that during control flow analysis of the loop, the type of the code variable is seen as 0 during an intermediate step. That's to be expected, but unfortunately it triggers an error that isn't actually correct once we arrive at the full type 0 | 1. We need to suppress that error, which we can do by tracking the fact that checkExpression was called from getTypeOfExpression (in which we care only about the type and not the diagnostics). I will put up a PR to that effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Fix Available A PR has been opened for this issue Rescheduled This issue was previously scheduled to an earlier milestone
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants