Skip to content

Conversation

@ahejlsberg
Copy link
Member

This PR fixes two issues:

  • Always error when non-optional properties in an object spread overwrite properties to the left. Previously we wouldn't error when the spread type included undefined.
  • Don't remove an explicitly declared undefined from a spread property type in --exactOptionalPropertyTypes mode.

Fixes #44438.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jun 22, 2021
for (const right of getPropertiesOfType(type)) {
const left = props.get(right.escapedName);
const rightType = getTypeOfSymbol(right);
if (left && !maybeTypeOfKind(rightType, TypeFlags.Nullable) && !(maybeTypeOfKind(rightType, TypeFlags.AnyOrUnknown) && right.flags & SymbolFlags.Optional)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandersn I'm not sure why we were checking nullable and any/unknown flags here. Seems to me any non-optional property that duplicates a property to the left should trigger the error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a corner case where a type that included undefined was actually for an optional property, but wasn't marked as such. For the others, it might be that unknown and (sort of) any include undefined.

We can ship this in the beta and see if people complain. It's likely that with better optionality tracking, this is not a problem.

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to ship a stricter check to go with the stricter optional properties. If people complain in the beta we can revisit the decision.

for (const right of getPropertiesOfType(type)) {
const left = props.get(right.escapedName);
const rightType = getTypeOfSymbol(right);
if (left && !maybeTypeOfKind(rightType, TypeFlags.Nullable) && !(maybeTypeOfKind(rightType, TypeFlags.AnyOrUnknown) && right.flags & SymbolFlags.Optional)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there was a corner case where a type that included undefined was actually for an optional property, but wasn't marked as such. For the others, it might be that unknown and (sort of) any include undefined.

We can ship this in the beta and see if people complain. It's likely that with better optionality tracking, this is not a problem.

@ahejlsberg ahejlsberg merged commit 61ccc49 into main Jun 22, 2021
@ahejlsberg ahejlsberg deleted the fix44438 branch June 22, 2021 21:39
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Object spread does not respect optional undefined union when strictOptionalProperties is on

4 participants