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

for-let + inner function incorrectly says variable is used before being assigned #61375

Open
monsanto opened this issue Mar 7, 2025 · 0 comments Β· May be fixed by #61376
Open

for-let + inner function incorrectly says variable is used before being assigned #61375

monsanto opened this issue Mar 7, 2025 · 0 comments Β· May be fixed by #61376
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@monsanto
Copy link

monsanto commented Mar 7, 2025

πŸ”Ž Search Terms

TS2454

πŸ•— Version & Regression Information

  • This changed between versions 5.6.3 and 5.7.3
  • This changed in commit or PR 533ed3d

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.2#code/PTAEFECdIe0goAZnUAKANgUwC6gB6gyKgDaAjADSgBMAugJSgDe8oboiArgHYDG2ASxjcOqRi3aT8AblbsAvvEXwQoAPIBpUAHcB2ABagAhtBjaOPfkO5IUGHPkLFyVOuLlssuYgF40jHwA+Zg8pPFlJRWVVTR09Q15hAGdsW0g0RO4UxyJSShoGEMkuPkFhUABzMSKpNnDQqKA

πŸ’» Code

// Error
for (let x of [1, 2]) {
    function f() {
        x;
    }
}

// OK with arrow function
for (let x of [1, 2]) {
    let f = () => {
        x;
    }
}

// OK with const
for (const x of [1, 2]) {
    function g() {
        x;
    }
}

πŸ™ Actual behavior

test.ts:5:9 - error TS2454: Variable 'x' is used before being assigned.

5         x;
          ~

let variables bound in the head of for are always assigned in the body.

πŸ™‚ Expected behavior

No errors.

Additional information about the issue

First noticed when upgrading from 5.6 to 5.7, was lazy and figured someone else would report it.

I figured TS might be incorrectly thinking f could be hoisted out of the loop, but it accepts it with const, so I don't know.

Commit found with

every-ts bisect start
every-ts bisect good v5.6.3
every-ts bisect bad v5.7.3
every-ts bisect run tsc --strict -m preserve test.ts -t esnext
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Mar 8, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 8, 2025
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
2 participants