Port JS specific code for this.xxx assignment declaration typing#3680
Merged
ahejlsberg merged 2 commits intomainfrom May 2, 2026
Merged
Port JS specific code for this.xxx assignment declaration typing#3680ahejlsberg merged 2 commits intomainfrom
this.xxx assignment declaration typing#3680ahejlsberg merged 2 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
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.getWidenedTypeForAssignmentDeclarationto ignore non-contributing assignment initializer types and to avoid producing an empty union. - Extends
getAssignmentDeclarationInitializerTypewith JSthis-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) |
jakebailey
approved these changes
May 1, 2026
Member
jakebailey
left a comment
There was a problem hiding this comment.
always love when we don't have tests for something
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.
This PR ports missing code for typing of
this.xxxassignment declarations in JavaScript. See here for analysis.Fixes #3667.