Skip to content

fix: skip block-scoped variable check for label identifiers#63365

Closed
Ankitajainkuniya wants to merge 2 commits intomicrosoft:mainfrom
Ankitajainkuniya:fix/label-block-scoped-error
Closed

fix: skip block-scoped variable check for label identifiers#63365
Ankitajainkuniya wants to merge 2 commits intomicrosoft:mainfrom
Ankitajainkuniya:fix/label-block-scoped-error

Conversation

@Ankitajainkuniya
Copy link
Copy Markdown

Summary

Fixes #30408

Labels and variables occupy separate namespaces in JavaScript/TypeScript. When a label name coincides with a later let/const variable declaration, the compiler incorrectly reports "Block-scoped variable 'foo' used before its declaration" on the label.

Repro

foo: while (true) {
    break foo;
}
let foo = 1;

Before: Error TS2448 on foo: and break foo — "Block-scoped variable 'foo' used before its declaration"
After: No error (correct behavior — foo: is a label, not a variable reference)

Fix

Added a guard in checkResolvedBlockScopedVariable (checker.ts) that skips the "used before declaration" check when the identifier is:

  • The label of a LabeledStatement (e.g., foo: while (...))
  • The label of a BreakStatement or ContinueStatement (e.g., break foo)

Regression Test

Normal "used before declaration" errors still fire correctly:

console.log(x); // TS2448: Block-scoped variable 'x' used before its declaration ✓
let x = 1;

Test Case

Added tests/cases/conformance/statements/labeledStatements/labeledStatementWithBlockScopedVariable.ts covering:

  • Label before let variable with break
  • Label before const variable with continue
  • Nested labeled statements with same-name variables

Labels and variables occupy separate namespaces in JavaScript/TypeScript.
When a label name coincides with a later let/const variable declaration,
the compiler incorrectly reports "Block-scoped variable used before its
declaration" on the label identifier or break/continue target.

This adds a guard in checkResolvedBlockScopedVariable to skip the check
when the error location is the label of a LabeledStatement, BreakStatement,
or ContinueStatement, since these reference labels, not variables.

Fixes microsoft#30408
@RyanCavanaugh
Copy link
Copy Markdown
Member

@Ankitajainkuniya what agent did you use to write this?

@Ankitajainkuniya
Copy link
Copy Markdown
Author

@Ankitajainkuniya what agent did you use to write this?

@RyanCavanaugh I used OpenAI as a coding assistant for parts of this, with my own review and modifications. The architecture decisions and product logic are mine the AI helped with implementation speed.

I'm building several open-source projects with this workflow: github.com/Ankitajainkuniya

@RyanCavanaugh
Copy link
Copy Markdown
Member

Please have your workflow ingest https://github.com/microsoft/TypeScript/blob/main/CONTRIBUTING.md before proceeding.

@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Confusing error message for labels used before definition

3 participants