Skip to content

Object.assign unexpectedly returns never type #44983

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

Closed
VVatashi opened this issue Jul 12, 2021 · 2 comments
Closed

Object.assign unexpectedly returns never type #44983

VVatashi opened this issue Jul 12, 2021 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@VVatashi
Copy link

Bug Report

🔎 Search Terms

Object.assign, intersection type, never type, undefined

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about intersection type and never type

⏯ Playground Link

Playground link with relevant code

💻 Code

I commonly use this pattern to "remove" unnecessary fields from an object in JS:

const foo = { a: 1, b: 2 };
const bar = Object.assign({}, foo, { b: undefined }); // Making a copy of 'foo' without field 'b'
bar.a = 1; // 'bar' should have at least 'a' field here, but TS gives error

🙁 Actual behavior

However, in TS the result of Object.assign was inferred to be of never type. So I got this error:

Property 'a' does not exist on type 'never'.
 The intersection '{ a: number; b: number; } & { b: undefined; }' was reduced to 'never' because property 'b' has conflicting types in some constituents.(2339)

Isn't this an error in definition of Object.assign function or in intersection type logic?

🙂 Expected behavior

I expected the result type to be something like { a: number; b?: number; }, { a: number; b: undefined; } or { a: number; }.

@MartinJohns
Copy link
Contributor

See the second and third point of @jcalz comment: #43110 (comment)

You can use a type assertion as a workaround: Object.assign({}, foo, { b: undefined as undefined | number });

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 12, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants