Skip to content

Skip default padding for params with declared/contextual types - #65542

Open
Jake Bailey (jakebailey) wants to merge 3 commits into
microsoft:mainfrom
jakebailey:fix-64431
Open

Jake Bailey (jakebailey) wants to merge 3 commits into
microsoft:mainfrom
jakebailey:fix-64431

Conversation

@jakebailey

Copy link
Copy Markdown
Member

#64043 made padObjectLiteralType add every binding element missing from a parameter's default object literal as an optional any property, and report implicit any for it. checkDeclarationInitializer pads whenever the root declaration is a parameter, including parameters whose type comes from an annotation, a JSDoc @type signature, or a contextual signature. For those parameters the initializer is only checked against that type, so the padded elements report spurious TS7031 errors:

type Options = (options?: { recursive?: boolean }) => void;
export const f: Options = ({ recursive } = {}) => {}; // TS7031 on 'recursive'

interface Outer { inner?: { b?: number } }
export function g({ inner: { b } = {} }: Outer) {} // TS7031 on 'b'

In nested patterns under a contextual type, the padded any property also eats the declared property type in the subtype-reduced union, so the element's type becomes any:

declare const items: { result?: { summary: number } }[];
const summaries = items.map(({ result: { summary } = {} }) => summary); // any[]

Pad object and tuple defaults only when the parameter gets its type from its initializer, meaning it has no annotation, JSDoc @type signature, 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 @type signatures on function declarations.

Fixes #64431
Fixes #64974

And the other fix attempts:

Closes #64440
Closes #64671
Closes #64978
Closes #65070

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 @type signatures, 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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: unknown

I 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

4 participants