Skip to content

using transform throws when a for body binding shadows the for head binding #61714

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

Open
JLHwung opened this issue May 16, 2025 · 4 comments
Open
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@JLHwung
Copy link

JLHwung commented May 16, 2025

🔎 Search Terms

explicit resource management, using, await using, loop initializer, Symbol.dispose, Symbol.asyncDispose, iteration, shadows

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.0-dev.20250516#code/MYGwhgzhAEBiD29oG8C+BYAUAM3gJ2gAoBXCASwDsBzaXJebaAbQF0BKFLaaYeCiAC61E0ALzQKAUwDucRITYBuLKiA

💻 Code

class Foo {}
for (using foo of []) {
  const foo = new Foo();
}

🙁 Actual behavior

The transformed output throws "redeclaration of const foo "

🙂 Expected behavior

The transformed output should not throw.

Additional information about the issue

Babel is also affected by this issue: babel/babel#17318. We plan to wrap the for body within a new block when it contains shadowed binding.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels May 16, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 16, 2025
@RyanCavanaugh
Copy link
Member

This seems like a bizarre thing to write; we might just choose to error instead.

@JLHwung
Copy link
Author

JLHwung commented May 16, 2025

This example is also valid JS and TS so far has no complaints:

class Foo {}
for (const foo of []) {
  const foo = new Foo();
}

If TS decides to error instead, this will introduce inconsistency between const and using.

@RyanCavanaugh
Copy link
Member

It seems like a likely code error to do that, though. You could have written

for ({} of []) {

instead

@JLHwung
Copy link
Author

JLHwung commented May 17, 2025

Yes, you are right. They are just valid pedantic examples. So I am calling for consistency. If TS thinks

for (using foo of []) {
  const foo = new Foo();
}

is bad and should throw an error (though currently the error is unintentional because of the transform result). Then the same error should be applied to

for (const foo of []) {
  const foo = new Foo();
}

as well.

The fact that the former is throwing while the latter is okay seems inconsistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy