Repository navigation
Skip default padding for params with declared/contextual types - #65542
Open
Jake Bailey (jakebailey) wants to merge 3 commits into
Open
Jake Bailey (jakebailey) wants to merge 3 commits into
Jake Bailey (jakebailey) wants to merge 3 commits into
Conversation
Padding a destructured parameter's default with its missing binding elements adds them as implicitly any, even when an annotation, a JSDoc @type signature, or a contextual type already types the parameter. That reports implicit any on elements that have types (64431, 64974), and under a contextual type a nested default's elements become any. Tuple defaults contextually typed by a tuple type have long reported the same errors.
Since 64043, padding an object default adds every missing binding element as an implicitly any property. That is meaningful only when the initializer gives the parameter its type. An annotation, a JSDoc @type signature, or a contextual type already types those elements, and the padded properties reported implicit any for them and replaced the types of elements in nested defaults. Fixes 64431 Fixes 64974
Tuple defaults have the same problem as object defaults. An `= []` default is a tuple when its contextual type is one, so a parameter contextually typed by `[string?, number?]` padded `[]` and reported implicit any for elements that the tuple type already types. This predates 7.0; applying the same rule keeps object and array defaults aligned.
Jake Bailey (jakebailey)
requested review from
Gabriela Araujo Britto (gabritto) and
Wesley Wigham (weswigham)
and
a balanced review from Copilot
November 16, 2026 21:12
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the destructured-parameter default typing fix.
No open issues 🎉
What changed in this PR
This PR fixes spurious TS7031 errors and lost binding-element types in destructured parameter defaults, addressing #64431 and #64974 in the native TypeScript checker.
Changes:
- Restricts object and tuple default padding to parameters without annotations, JSDoc
@typesignatures, or contextual types. - Adds regression coverage for contextual, annotated, nested, and JSDoc-typed destructuring.
- Updates baselines to reflect preserved types and removal of spurious implicit-any diagnostics.
| File | Description |
|---|---|
tsc/testdata/tests/cases/compiler/objectBindingPatternDefaultMissingElements.ts |
Adds regression cases for typed parameter defaults. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.types |
Records preserved binding-element types. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.symbols |
Records symbols for the added cases. |
tsc/testdata/baselines/reference/compiler/objectBindingPatternDefaultMissingElements.errors.txt |
Captures expected diagnostics for the expanded tests. |
tsc/testdata/baselines/reference/compiler/inferredRestTypeFixedOnce.errors.txt |
Removes the obsolete TS7031 error baseline. |
tsc/internal/checker/checker.go |
Guards padding based on the source of the parameter’s type. |
Review effort: Balanced
Contributor
There was a problem hiding this comment.
Isn't this a regression though? There is really no source of explicit any here:
function wrap<Args extends unknown[]>(_: (...args: Args) => void) {}
wrap(({ cancelable } = {}) => {});
// ^? (parameter) cancelable: any
wrap((param = {}) => {});
// ^? (parameter) param: unknownI don't recall 100% right now but IIRC this any type might accidentally leak from here with reportErrors: false:
https://github.com/microsoft/TypeScript/blob/68672025cc6f01c845051d9f9b04200b0382942c/tsc/internal/checker/checker.go#L10685-L10688
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.
#64043 made
padObjectLiteralTypeadd every binding element missing from a parameter's default object literal as an optionalanyproperty, and report implicit any for it.checkDeclarationInitializerpads whenever the root declaration is a parameter, including parameters whose type comes from an annotation, a JSDoc@typesignature, or a contextual signature. For those parameters the initializer is only checked against that type, so the padded elements report spurious TS7031 errors:In nested patterns under a contextual type, the padded
anyproperty also eats the declared property type in the subtype-reduced union, so the element's type becomesany:Pad object and tuple defaults only when the parameter gets its type from its initializer, meaning it has no annotation, JSDoc
@typesignature, or contextual type. This builds on #65070 by Timur Vural (@timurzvural), which uses the guard first proposed in #64978 by Oleksandr Tarasiuk (@a-tarasyuk), but extends it to JSDoc@typesignatures on function declarations.Fixes #64431
Fixes #64974
And the other fix attempts:
Closes #64440
Closes #64671
Closes #64978
Closes #65070