Fix TS2783 false positive for union types in object spread expressions - #62656
Merged
Ryan Cavanaugh (RyanCavanaugh) merged 5 commits intoOct 22, 2025
Merged
Conversation
Copilot started work on behalf of
Ryan Cavanaugh (RyanCavanaugh)
October 22, 2025 05:30
View session
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix TS2783 false positive issue
Fix TS2783 false positive for union types in object spread expressions
Oct 22, 2025
Member
|
TypeScript Bot (@typescript-bot) test it |
Contributor
Contributor
|
Hey Ryan Cavanaugh (@RyanCavanaugh), the results of running the DT tests are ready. Everything looks the same! |
Contributor
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
Contributor
|
Ryan Cavanaugh (@RyanCavanaugh) Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contributor
|
Ryan Cavanaugh (@RyanCavanaugh) Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
Ryan Cavanaugh (RyanCavanaugh)
approved these changes
Oct 22, 2025
Jake Bailey (jakebailey)
marked this pull request as ready for review
October 22, 2025 19:04
Fix issue ref
Andrew Branch (andrewbranch)
approved these changes
Oct 22, 2025
Ryan Cavanaugh (RyanCavanaugh)
deleted the
copilot/fix-ts2783-false-positive
branch
October 22, 2025 23:44
Copilot AI
added a commit
to microsoft/typescript-go
that referenced
this pull request
Feb 10, 2026
Port of microsoft/TypeScript#62656. Adds CheckFlags.Partial check in checkSpreadPropOverrides so that properties present in only some union constituents don't trigger the "always overwrites" error. Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #61223
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:
The error claimed
idwould "always" be overwritten, but this was incorrect. The spread expression evaluates toThing | { label: string }, and the fallback object{ label: 'Foo' }doesn't have anidproperty at all, soidis only overwritten whenfind()returns aThing.Root Cause
The
checkSpreadPropOverridesfunction only checked if a property hadSymbolFlags.Optionalbefore 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 withCheckFlags.Partial(specificallyReadPartialorWritePartial), indicating they're only present in a subset of the union's constituents.Solution
Added an additional check for
CheckFlags.PartialincheckSpreadPropOverrides. Properties marked as partial in union types should not trigger the "always overwrites" error since they don't exist in all code paths.Test Coverage
Added comprehensive test file
spreadUnionPropOverride.tsthat verifies:Thing | { label: string }no longer errors onidThe fix preserves all existing correct behavior while eliminating the false positive for partial properties in union types.
Original prompt
Fixes #62655
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.