Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spread object type inference error #51253

Closed
wmzy opened this issue Oct 21, 2022 · 4 comments
Closed

Spread object type inference error #51253

wmzy opened this issue Oct 21, 2022 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@wmzy
Copy link

wmzy commented Oct 21, 2022

Bug Report

πŸ”Ž Search Terms

πŸ•— Version & Regression Information

TS version: 4.8.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type T = {
  foo?: string;
};

const a: T = {foo: undefined};
const b = {foo: '', ...a};

πŸ™ Actual behavior

image

πŸ™‚ Expected behavior

The type of b should be:

{
  foo?: string;
}
@fatcerberus
Copy link

There's no reason to infer foo be optional in b - there will always be a foo property, even if a doesn't have one, because you've provided it upfront. The unsoundness here is that undefined is assignable to optional properties - you can enable exactOptionalPropertyTypes to prevent that.

@fatcerberus
Copy link

fatcerberus commented Oct 21, 2022

btw this is not destructuring assignment - it’s a spread. Destructuring is when you do

let { x, y } = point;

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 21, 2022

@fatcerberus Object spread only skips properties that haven't been defined, but it does not skip properties that have been defined to be undefined. Confusing, I know. The object ends up being { foo: undefined }, so the type string is wrong.

But I'm certain there's an existing issue for this that I can't find right now.

edit: Just noticed that you mentioned exactOptionalPropertyTypes, which is probably what I was thinking of and shows that you know about what I just said.

edit: This is a duplicate of #44432

@fatcerberus
Copy link

@MartinJohns Yes, I was aware. Without exactOptionalPropertyTypes, optional properties can be set to undefined, but the type system still generally treats the property as if it’s either present-with-the-given-type or else non-present. This is indeed unsound, but the fix is to enable exactOptionalPropertyTypes. πŸ˜„

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Oct 21, 2022
@wmzy wmzy changed the title Destructuring assignment type inference error Spread object type inference error Oct 22, 2022
@wmzy wmzy closed this as completed Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants