Skip to content

Increment expression of for loops don't invalid the type of variables typed as literal number values #50176

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
MichaelMitchell-at opened this issue Aug 4, 2022 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@MichaelMitchell-at
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

increment decrement for loop variable mutate const literal number invalidate invalidation stale

πŸ•— Version & Regression Information

This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

for (let i = 0 as const; i < 10;) {
  // error - expected
  const x: 0 = i++;
}

for (let i = 0 as const; i < 10;) {
  i++;
  // error - expected
  const x: 0 = i;
}

for (let i = 0 as const; i++ < 10;) {
  // error - expected
  const x: 0 = i;
}

for (let i = 0 as const; i < 10; i++) {
  // no error - unexpected!
  const x: 0 = i;
}

πŸ™ Actual behavior

No type error

πŸ™‚ Expected behavior

Type error, since this is obviously unsound.

@MartinJohns
Copy link
Contributor

Duplicate of #14745.

@MichaelMitchell-at
Copy link
Contributor Author

MichaelMitchell-at commented Aug 4, 2022

I'd contend that this isn't a duplicate on the grounds that even though #14745 was closed as "Won't fix", the example that it referenced was actually fixed at some point in the last five years, but the example I provided still doesn't work.

@MartinJohns
Copy link
Contributor

MartinJohns commented Aug 4, 2022

There's also #47027, which is essentially about the same issue and is still open. The issue is that the operators that modify the unary / left-side argument can be applied on literal typed variables.

@fatcerberus
Copy link

The increment and subsequent assignment being allowed is #47027, but there seems to be a second bug here: removing i++ from the loop header makes i be treated as number inside the loop (thus all the other assignments being errors), while having it there preserves the literal type. That is… quite bizarre and if anything I’d expect it to be the other way around.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 4, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants