Skip to content

Contextually type array literal spread operands - #64489

Open
Sahil Singh Kushwah (WhitefistEmperor) wants to merge 2 commits into
microsoft:mainfrom
WhitefistEmperor:fix/contextual-type-array-spreads
Open

Sahil Singh Kushwah (WhitefistEmperor) wants to merge 2 commits into
microsoft:mainfrom
WhitefistEmperor:fix/contextual-type-array-spreads

Conversation

@WhitefistEmperor

@WhitefistEmperor Sahil Singh Kushwah (WhitefistEmperor) commented Sep 28, 2026 •

Copy link
Copy Markdown

Fixes #45600

Summary

Array literals used as spread operands were checked without the contextual type of the containing array or call. This caused literal properties to widen prematurely—for example, { field: "a" } became { field: string }—and produced an incorrect assignability error.

This change propagates the relevant array or tuple context into spread operands.

Approach

  • Derive the contextual type for an array spread from the portion of the containing array or tuple that it occupies.
  • Use the remaining parameter types to contextually type literal spread operands when a call has one non-generic candidate signature.
  • Support parenthesized, conditional, ||, and ?? expression trees containing array literals.
  • Preserve existing behavior for ordinary arrays and custom iterators by restricting signature-aware handling to contextually typable literal expression trees.

This also improves tuple precision in an existing Promise.all case from [number, ...void[]] to [number, void] and resolves an existing literal-freshness case marked Desired: OK.

Tests

Added regression coverage for:

  • direct nested array spreads
  • conditional spreads
  • tuple slices
  • tuples constructed from a single spread
  • multiple spreads
  • rest-parameter calls
  • fixed-parameter calls
  • invalid elements that must continue to produce an error

The new regression produces no diagnostics for valid cases and exactly one TS2322 diagnostic for the intentional invalid control.

Validation performed:

  • focused regression test
  • 26 related compiler regression tests
  • complete compiler baseline suite
  • native-preview tests: 12/12 passed
  • compiler and tooling lint: 0 issues
  • generation and formatting checks

npx hereby validate completed all compiler, native-preview, lint, generation, and formatting stages successfully. The only local failures were the Windows fswatch symlink tests because the current account does not have permission to create symbolic links.

AI assistance disclosure

I used OpenAI Codex to assist with investigating, implementing, and testing this change. I reviewed the resulting implementation and tests, understand the design and tradeoffs, and will personally respond to review feedback.

Copilot AI balanced review requested due to automatic review settings September 28, 2026 06:39
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Sep 28, 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.

Copilot review overview

🟡 Changes recommended

Multiple call spreads can receive parameter context from the wrong position, and two new expression paths lack regression coverage.

Review effort: Balanced
Findings: 1 High severity · 1 Low severity

Open (2)
What changed in this PR

Propagates contextual types into array spread operands to preserve literal and tuple precision.

Changes:

  • Adds spread-context derivation for arrays, tuples, and calls.
  • Supports nested conditional/logical literal expressions.
  • Adds regression tests and updates affected baselines.
File Description
tsc/​internal/​checker/​checker.go Implements contextual spread typing.
tsc/​internal/​checker/​utilities.go Updates call-argument API usage.
tsc/​testdata/​tests/​cases/​compiler/​contextualTypingArraySpreadElements.ts Adds regression cases.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypingArraySpreadElements.types Records inferred types.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypingArraySpreadElements.symbols Records symbols.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypingArraySpreadElements.js Records emitted JavaScript.
tsc/​testdata/​baselines/​reference/​compiler/​contextualTypingArraySpreadElements.errors.txt Records expected diagnostic.
tsc/​testdata/​baselines/​reference/​compiler/​literalFreshnessPropagationOnNarrowing.types Updates literal freshness types.
tsc/​testdata/​baselines/​reference/​compiler/​literalFreshnessPropagationOnNarrowing.errors.txt Removes resolved diagnostic.
tsc/​testdata/​baselines/​reference/​compiler/​awaitedType.types Updates Promise.all tuple precision.

Comment thread tsc/internal/checker/checker.go Outdated
Comment thread tsc/internal/checker/checker.go Outdated
@WhitefistEmperor

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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 Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Contextual type doesn't apply to elements of array literal spread into another array literal

2 participants