Skip to content

fix(65069): preserve contextual types for array-destructured parameter defaults - #65070

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

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

Conversation

@timurzvural

Copy link
Copy Markdown

Fixes #65069.

Bug

In 7.1, a destructured parameter with a default like = [] loses its contextual type and reports TS7031 on binding elements. The object form (= {}) is #64974; this PR covers the array form and also guards object padding the same way.

type F = (input?: [string?, number?]) => number;
export const f: F = ([a, b] = []) => (a ? 1 : 0) + (b ?? 0);

Fix

In checkDeclarationInitializer, only call padTupleType / padObjectLiteralType when getContextualTypeForVariableLikeDeclaration(root, ContextFlagsNone) == nil. Padding invents any-typed missing elements and must not override a contextual parameter type.

Tests

Extended objectBindingPatternDefaultMissingElements.ts (and baselines) with array/= [] cases from #65069 and an object/= {} case.

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.

…r defaults

Skip padTupleType when the parameter already has a contextual type, matching
the object-pattern guard for microsoft#64974. Also apply the same guard to
padObjectLiteralType so object `= {}` defaults keep working on the same path.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 01:01
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Oct 31, 2026
@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 31, 2026

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

The change preserving contextual types in destructured parameter defaults has no active review issues.

No open issues 🎉

What changed in this PR

Fixes #65069 by preserving contextual types for array-destructured parameter defaults in the native TypeScript checker. The same guard also addresses object defaults from #64974.

Changes:

  • Skip tuple and object padding when the parameter has a contextual type.
  • Add regression cases for object defaults, array defaults, callbacks, and nested/rest destructuring.
  • Update type, symbol, and diagnostic baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​objectBindingPatternDefaultMissingElements.ts Adds contextual-typing regression cases.
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 Records expected assignment errors that verify contextual typing.
tsc/​internal/​checker/​checker.go Guards object and tuple padding with a contextual-type check.

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.

7.1: array-destructured parameter with a = [] default loses its contextual type (TS7031)

2 participants