No widening to any[] when parent of expando has type annotation#3986
Merged
Conversation
Closed
4 tasks
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the checker’s expando-property typing so that assigning an empty array literal ([]) to an expando property does not widen to any[] when the expando object’s containing declaration has an explicit type annotation. This aligns tsgo behavior with Strada and fixes the regression reported in #3976.
Changes:
- Updates expando assignment initializer typing to skip the “empty array => widen to
any[]+ implicit-any error” path when the expando parent is type-annotated. - Adds a compiler regression test covering callable objects with fields (typed vs untyped) and expected TS7008 diagnostics/baselines.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
internal/checker/checker.go |
Skips empty-array widening to any[] for expando assignment declarations when the expando parent is type-annotated. |
testdata/tests/cases/compiler/expandoPropertyEmptyArrayWidening.ts |
New regression test reproducing #3976 and asserting correct behavior for typed vs untyped expandos. |
testdata/baselines/reference/compiler/expandoPropertyEmptyArrayWidening.* |
New reference baselines (types/symbols/errors) validating the updated behavior. |
Comment on lines
+17999
to
+18005
| // Return true if the parent symbol of the given assignment declaration symbol has declaration with a type | ||
| // annotation. For example, returns true for the symbol associated with `f.a` below: | ||
| // | ||
| // const f: { (): void, a: string[] } = () => {}; | ||
| // f.a = []; | ||
| func (c *Checker) hasParentWithTypeAnnotation(symbol *ast.Symbol) bool { | ||
| if symbol.Parent != nil && symbol.Parent.ValueDeclaration != nil && ast.IsFunctionExpressionOrArrowFunction(symbol.Parent.ValueDeclaration) { |
RyanCavanaugh
approved these changes
May 19, 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.
With this PR we don't widen empty array literals to
any[]in expando property assignments when the expando object has a type annotation. This mirrors the behavior in Strada. Part of me wants to just treat such expando property assignments the same as property assignments in object literals, meaning that we'd type the literals asnever[]whennoImplicitAny: true, but that would be a breaking change.Fixes #3976.