Port PR #61376: Fix spurious "used before being assigned" errors in for of/in loops#2462
Merged
RyanCavanaugh merged 3 commits intomainfrom Jan 21, 2026
Merged
Conversation
Port of microsoft/TypeScript#61376. Adds check for isForInOrOfStatement to prevent false positive TS2454 errors for variables declared in for-in/for-of loops when referenced in closures within the loop body. Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Port changes from TypeScript PR 61376
Port PR #61376: Fix spurious "used before being assigned" errors in for of/in loops
Jan 9, 2026
RyanCavanaugh
approved these changes
Jan 9, 2026
Member
RyanCavanaugh
left a comment
There was a problem hiding this comment.
Diff looks wonky because of the line breaks, what's actually happening is this
-isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && immediateDeclaration.Initializer() == nil && immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil && c.isMutableLocalVariableDeclaration(immediateDeclaration) && !c.isSymbolAssignedDefinitely(symbol)
+isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && !ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent) && immediateDeclaration.Initializer() == nil && immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil && c.isMutableLocalVariableDeclaration(immediateDeclaration) && !c.isSymbolAssignedDefinitely(symbol)
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request ports TypeScript PR #61376, which fixes spurious "used before being assigned" errors (TS2454) for variables declared in for-in/for-of loops when they are referenced in closures within the loop body.
Key Changes:
- Added a check
!ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent)to theisNeverInitializedcondition incheckIdentifierfunction - Variables in for-in/for-of loops are now correctly recognized as implicitly initialized by the loop iteration
jakebailey
approved these changes
Jan 15, 2026
RyanCavanaugh
requested changes
Jan 15, 2026
Member
RyanCavanaugh
left a comment
There was a problem hiding this comment.
@copilot also add the new tests from the upstream PR as local tests in this repo
Added test from upstream PR microsoft/TypeScript#61376 to verify the fix for spurious "used before being assigned" errors (TS2454) in for-in/of loops. Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Contributor
Author
jakebailey
approved these changes
Jan 15, 2026
RyanCavanaugh
approved these changes
Jan 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Port of microsoft/TypeScript#61376.
Variables declared in for-in/for-of loops were incorrectly triggering TS2454 ("used before being assigned") when referenced in closures within the loop body.
Changes
!ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent)check toisNeverInitializedininternal/checker/checker.gounusedLocalsInForInOrOf1.tsfrom upstream PR with baselinesVariables in for-in/for-of loops are implicitly initialized by the loop iteration and should not be treated as uninitialized.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.