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

Incorrect Object.assign return type #45215

Closed
AndreyKuznetsovQS opened this issue Jul 28, 2021 · 3 comments
Closed

Incorrect Object.assign return type #45215

AndreyKuznetsovQS opened this issue Jul 28, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@AndreyKuznetsovQS
Copy link

Bug Report

πŸ”Ž Search Terms

object.assign return type

πŸ•— Version & Regression Information

Faced this problem in the 4.3.2 version and checked on 4.3.5, 4.4.0-beta(problem still exists)

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type SomeData = { a: number };

const getSomeData = (): SomeData | undefined => {
  return undefined;
}

// expected data type: SomeData | undefined
// actual data type: SomeData
const data = Object.assign({}, getSomeData())

πŸ™ Actual behavior

Incorrect type inference

πŸ™‚ Expected behavior

Correct type inference

@MartinJohns
Copy link
Contributor

// expected data type: SomeData | undefined

Why would you expect undefined? That can never be the result. I'd rather expect something like { a?: number }.

@jcalz
Copy link
Contributor

jcalz commented Jul 28, 2021

The return type of Object.assign() is modeled as the intersection of the types of the input values, which is a rough approximation of what actually happens. It's a simple heuristic and is good enough to be useful, but there are edge cases (see #31982, #44983, #45034, #43110). It is possible to get a better approximation, but at the expense of being a lot more complicated and so far nobody has decided it's worth it to do that.

In this example, the compiler sees Object.assign({}, undefined) as returning {} & undefined which is never, while what actually gets returned is of type {} (not undefined, as @MartinJohns points out). So the right return type for Object.assign({}, getSomeData()) is something like SomeData | {} (or possibly Partial<SomeData> since you know that the value {} has no a property).

In the likely case that this will be closed as a design limitation, you should probably just work around it by asserting it like Object.assign({}, getSomeData()) as Partial<SomeData>.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 29, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants