Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 22, 2025

This PR ports microsoft/TypeScript#62656 to fix a false positive TS2783 error when spreading union types in object literals.

Problem

TypeScript incorrectly reported error TS2783 ("'x' is specified more than once, so this usage will be overwritten") when spreading union types in object literals, even when the property wasn't guaranteed to exist in all constituents of the union.

For example, this code incorrectly produced an error:

type Thing = {
    id: string;
    label: string;
};

function find(id: string): undefined | Thing {
    return things.find(thing => thing.id === id);
}

fun({
    id: 'foo',
    ...find('foo') ?? { label: 'Foo' },  // ❌ False positive: 'id' is specified more than once
});

The error claimed id would "always" be overwritten, but this was incorrect. The spread expression evaluates to Thing | { label: string }, and the fallback object { label: 'Foo' } doesn't have an id property at all, so id is only overwritten when find() returns a Thing.

Root Cause

The checkSpreadPropOverrides function only checked if a property had SymbolFlags.Optional before reporting the error. It didn't account for properties that exist in some constituents of a union type but not others. These properties are marked with CheckFlags.Partial (specifically ReadPartial or WritePartial), indicating they're only present in a subset of the union's constituents.

Solution

Added an additional check for CheckFlags.Partial in checkSpreadPropOverrides. Properties marked as partial in union types should not trigger the "always overwrites" error since they don't exist in all code paths.

// Only report error if property is both non-optional AND not partial
if right.Flags&ast.SymbolFlagsOptional == 0 && right.CheckFlags&ast.CheckFlagsPartial == 0 {
    // Report error...
}

Testing

  • All existing tests pass with no baseline changes
  • Manually verified correct behavior for:
    • Union types where property exists in only some constituents (no error - fixed)
    • Union types where property exists in all constituents (error - preserved)
    • Optional properties in unions (no error - preserved)
    • Properties with union types like x: number | undefined (error - preserved)

The fix preserves all existing correct behavior while eliminating the false positive for partial properties in union types.

Fixes microsoft/TypeScript#61223

Original prompt

Port PR 62656

Note

Custom agent used: Strada to Corsa Port Expert
A Go and TypeScript expert who can easily figure out how to port PRs from one language to another


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Port changes from PR 62656 to main branch Port PR 62656: Fix TS2783 false positive for union types in object spread expressions Oct 22, 2025
Copilot AI requested a review from RyanCavanaugh October 22, 2025 19:32
Copilot finished work on behalf of RyanCavanaugh October 22, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Warn when tsconfig.json extends a file that contains references

2 participants