Skip to content

Commit 07198ac

Browse files
Fix spurious "used before being assigned" errors in for of/in loops
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>
1 parent 681501e commit 07198ac

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

internal/checker/checker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10806,9 +10806,9 @@ func (c *Checker) checkIdentifier(node *ast.Node, checkMode CheckMode) *Type {
1080610806
// We only look for uninitialized variables in strict null checking mode, and only when we can analyze
1080710807
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
1080810808
// declaration container are the same).
10809-
isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && immediateDeclaration.Initializer() == nil &&
10810-
immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil && c.isMutableLocalVariableDeclaration(immediateDeclaration) &&
10811-
!c.isSymbolAssignedDefinitely(symbol)
10809+
isNeverInitialized := immediateDeclaration != nil && ast.IsVariableDeclaration(immediateDeclaration) && !ast.IsForInOrOfStatement(immediateDeclaration.Parent.Parent) &&
10810+
immediateDeclaration.Initializer() == nil && immediateDeclaration.AsVariableDeclaration().ExclamationToken == nil &&
10811+
c.isMutableLocalVariableDeclaration(immediateDeclaration) && !c.isSymbolAssignedDefinitely(symbol)
1081210812
assumeInitialized := isParameter ||
1081310813
isAlias ||
1081410814
(isOuterVariable && !isNeverInitialized) ||

0 commit comments

Comments
 (0)