Repository navigation
fix(65011): preserve contextual types for destructured parameter defaults - #65016
Open
Timur Vural (timurzvural) wants to merge 1 commit into
Open
Timur Vural (timurzvural) wants to merge 1 commit into
Timur Vural (timurzvural) wants to merge 1 commit into
Conversation
…ults
When a parameter uses a binding pattern with `= {}` / `= []`, do not pad
the initializer's object/tuple type if the parameter already has a
contextual type. Padding invented any-typed properties and caused TS7031
on binding elements (7.1 regression vs 7.0).
Also covers the related case from microsoft#64974. Adds compiler tests for the
JSDoc/@returns and annotated-function shapes from microsoft#65011.
AI assistance disclosure: authored with Cursor Agent.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues block approval of the contextual-typing regression fix.
No open issues 🎉
What changed in this PR
Fixes the TypeScript 7.1 regression in #65011 and #64974 where destructured parameter defaults lose contextual types and incorrectly report TS7031.
Changes:
- Skips object and tuple padding when the parameter already has a contextual type.
- Adds regression cases and baselines covering TypeScript callbacks, nested destructuring, array patterns, and JSDoc contextual typing.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts |
Expands contextual typing and destructuring coverage. |
tsc/testdata/tests/cases/compiler/contextualDestructuredParameterDefault.ts |
Adds JSDoc and TypeScript cases from #65011. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types |
Records inferred types for expanded cases. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols |
Records symbol bindings for expanded cases. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt |
Records expected diagnostics for negative cases. |
tsc/testdata/baselines/reference/compiler/contextualDestructuredParameterDefault.types |
Records preserved contextual parameter types. |
tsc/testdata/baselines/reference/compiler/contextualDestructuredParameterDefault.symbols |
Records symbols for the new regression cases. |
tsc/internal/checker/checker.go |
Guards parameter initializer padding against overriding contextual types. |
Review effort: Balanced
This branch has not been deployed
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.
Bug
In 7.1, a destructured parameter with a default like
= {}(or= []) loses its contextual type and reportsTS7031on binding elements. This worked in 5.9 and 7.0.Example (also fails for the same shape in
.ts):Root cause
checkDeclarationInitializeralways calledpadObjectLiteralType/padTupleTypefor parameter binding patterns. Padding inventsany-typed missing properties and overrides the contextual parameter type.Fix
Only pad when
getContextualTypeForVariableLikeDeclaration(root, ContextFlagsNone) == nil, for both object and array binding patterns.Tests
objectBindingPatternDefaultMissingElements.ts(+ baselines) with cases from 7.1: destructured parameter with a= {}default loses its contextual type (TS7031) #64974 / Regression in 7.1: contextual parameter type is lost for a destructuring pattern with a default (TS7031) #65011contextualDestructuredParameterDefault.ts(+ symbols/types baselines) covering the JSDoc@returnsmethod, cast, and.tsannotation shapes from Regression in 7.1: contextual parameter type is lost for a destructuring pattern with a default (TS7031) #65011Related
= {}default loses its contextual type (TS7031) #64974AI assistance disclosure
This PR was authored with assistance from Cursor Agent. I have read and understand the change and am prepared to discuss and revise it in review.