Skip to content

fix(65011): preserve contextual types for destructured parameter defaults - #65016

Open
Timur Vural (timurzvural) wants to merge 1 commit into
microsoft:mainfrom
timurzvural:fix/65011-contextual-destructured-default
Open

Timur Vural (timurzvural) wants to merge 1 commit into
microsoft:mainfrom
timurzvural:fix/65011-contextual-destructured-default

Conversation

@timurzvural

Copy link
Copy Markdown

Bug

In 7.1, a destructured parameter with a default like = {} (or = []) loses its contextual type and reports TS7031 on binding elements. This worked in 5.9 and 7.0.

Example (also fails for the same shape in .ts):

/** @returns {Adapter} */
export function make() {
  return {
    async structure(c, { signal } = {}) {
      signal?.throwIfAborted()
      return [c]
    },
  }
}

Root cause

checkDeclarationInitializer always called padObjectLiteralType / padTupleType for parameter binding patterns. Padding invents any-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

Related

AI 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.

…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.

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

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

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Regression in 7.1: contextual parameter type is lost for a destructuring pattern with a default (TS7031)

2 participants