Port PR 62656: Fix TS2783 false positive for union types in object spread expressions #1932
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.
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:
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.Testing
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
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.