Skip to content

Fix incorrect error location when a spread overrides an explicit property (#51376) - #64480

Open
Alok Sharma (im-alok74) wants to merge 1 commit into
microsoft:mainfrom
im-alok74:fix-spread-elaboration
Open

Alok Sharma (im-alok74) wants to merge 1 commit into
microsoft:mainfrom
im-alok74:fix-spread-elaboration

Conversation

@im-alok74

Copy link
Copy Markdown

Fixes #51376

Problem

When an object literal has an explicit property later overwritten by a
spread (e.g. { attribute: "", ...result }), and the merged type causes
an assignability error, the checker's object literal elaboration
(elaborateObjectLiteral in relater.go) always attributed the
diagnostic to the first node it found with that property name — the
earlier explicit property — even when a later spread in the same
literal also contributes (and overrides) that property in the merged
type. This pointed the error at code that was actually fine, while the
real source of the type mismatch (the spread) went unmarked.

Fix

elaborateObjectLiteral now precomputes, for each spread in the object
literal, the set of property names it contributes to the merged type.
When elaborating an explicit property, it checks whether any later
spread also declares that property name; if so, the diagnostic is
anchored to that spread's expression instead of the (now shadowed)
property, and the property's own initializer is no longer recursed
into for elaboration purposes.

Testing

  • Added spreadOverridingPropertyElaboratesOnSpread.ts with three
    cases: the fixed case (spread after property), a control where the
    property comes after the spread (no error), and a control with no
    spread at all (error still on the property).
  • Verified manually against both repros discussed in the issue thread:
    the original report's Partial<Record<'attribute', string | null>>
    case, and the later "clearer" repro (const result = { attribute: 0 })
    — both now report TS2322 on the spread expression, not on the
    explicit property.
  • Full go test ./internal/testrunner/... -run TestLocal suite passes
    with no regressions.

… shadowed property

When a later spread in an object literal overwrites an earlier explicit
property of the same name in the merged type, elaborateObjectLiteral was
still elaborating the assignability error against the earlier property's
initializer. This pointed diagnostics at the wrong source location, since
the spread - not the shadowed property - determines the property's final
type.

Fixes microsoft#51376
Copilot AI balanced review requested due to automatic review settings September 27, 2026 12:54
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 27, 2026
@typescript-automation typescript-automation Bot added For Backlog Bug PRs that fix a backlog bug labels Sep 27, 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 was unable to review this pull request because the user who requested the review has reached their quota limit.

@im-alok74

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.

Spread operator with wrong optional property raises error on incorrect source line

2 participants