Skip to content

Port JS specific code for this.xxx assignment declaration typing#3680

Merged
ahejlsberg merged 2 commits intomainfrom
fix-3667
May 2, 2026
Merged

Port JS specific code for this.xxx assignment declaration typing#3680
ahejlsberg merged 2 commits intomainfrom
fix-3667

Conversation

@ahejlsberg
Copy link
Copy Markdown
Member

This PR ports missing code for typing of this.xxx assignment declarations in JavaScript. See here for analysis.

Fixes #3667.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Ports missing TypeScript checker logic needed to correctly infer types for JavaScript this.<prop> assignment declarations (especially around self-referential assignments and empty array literals), aligning tsgo behavior with tsc for issue #3667.

Changes:

  • Adds a new compiler test case covering this.<prop> assignment-declaration type inference across constructors vs methods (and empty array literal behavior).
  • Updates Checker.getWidenedTypeForAssignmentDeclaration to ignore non-contributing assignment initializer types and to avoid producing an empty union.
  • Extends getAssignmentDeclarationInitializerType with JS this-property self-reference detection and correct empty-array widening/diagnostics behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

File Description
internal/checker/checker.go Adjusts JS assignment-declaration type inference to skip self-referential this.<prop> initializers and handle empty-type unions safely.
testdata/tests/cases/compiler/thisPropertyAssignmentTyping.ts New regression test covering constructor vs method typing for this.<prop> assignment declarations in JS.
testdata/baselines/reference/compiler/thisPropertyAssignmentTyping.* Adds expected baselines (types/symbols/errors) for the new test.

}
}

// When this.xxx assigmment declarations occur only in methods, an assigned empty array
this.bar = []; // Error: Implicit any[]
this.bar.push("baz");
this.bar; // any[]
}
Comment on lines +17763 to +17766
if ast.IsFunctionLike(node) {
return false
}
return node.ForEachChild(visit)
Copy link
Copy Markdown
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always love when we don't have tests for something

@ahejlsberg ahejlsberg added this pull request to the merge queue May 2, 2026
Merged via the queue into main with commit 2f6504c May 2, 2026
25 checks passed
@ahejlsberg ahejlsberg deleted the fix-3667 branch May 2, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Behavior difference: Assigning array to uninitialized class members yields different error on tsc & tsgo

3 participants