question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fatcerberuscommented, Oct 23, 2021

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.

2reactions
fatcerberuscommented, Oct 22, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variable gets expanded in a loop with condition, how to "reset ...
I have a loop that scans through each data point and checks for a certain condition: for i in range(0, 10): if x...
Read more >
Can I nest an if statement in a for loop? - MATLAB Answers
Yes, you can put an if statement inside a for loop. Your syntax for the for loop is invalid though, not the if...
Read more >
for Loop
Condition is an expression that is tested each time the loop repeats. As long as condition is true, the loop keeps running. Increment...
Read more >
Loops and Strings | Think Java | Trinket
The loops we have written so far have three parts in common. They start by initializing a variable, they have a condition that...
Read more >
Javanotes 9, Section 3.4 -- The for Statement
The value of the variable is tested in the continuation condition, and the loop ends when this condition evaluates to false. A variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found