-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Return types of Object.assign #45034
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
Comments
In the first case the intersection could be collapsed but I donβt think itβs possible to represent the second case in the type system. |
Type display is allowed to choose among equivalent representations so this isn't a bug. Not eagerly collapsing unions/intersections is generally the behavior preferred by users and is computationally cheaper, so this is expected or at least justifiable. I agree with @fatcerberus there isn't a better representation of what to do with primitive passed into |
Might help if I show my use case for this rather than an abstract example: export const createCanvas = (width: number, height: number) =>
globalThis.OffscreenCanvas
? new OffscreenCanvas(width, height)
: Object.assign(document.createElement('canvas'), { width, height }) Creates an To be honest it's a minor irritation and can be fixed by adding a type annotation, but definitely seems like the naively expected behavior in this case would be that the function's return type would simply be Also, you get no indication that there's a bug in this modified version: export const createCanvas = (widht: number, heigth: number) =>
globalThis.OffscreenCanvas
? new OffscreenCanvas(widht, heigth)
: Object.assign(document.createElement('canvas'), { widht, heigth }) Not saying that modified version should be a type error (it shouldn't), but it'd be helpful if there was some easily-visible way in which its results differed from the correct version. |
|
Yes, it's functionally equivalent. The only difference is that intellisense displays the type as |
I think
|
Bug Report
π Search Terms
Object.assign
π Version & Regression Information
4.3.2
Object.assign
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
This may be 2 separate bugs, but I suspect they may be related.
createFoo
above, the returned type specifies& { bar: number }
, even though{ bar: number }
is already part of theFoo
type.markSafe
above, the returned type specifiesstring
, even though what is actually returned is aString
object with the additional property added, not a primitivestring
(so, for example,markSafe('') !== markSafe('')
).π Expected behavior
createFoo
return type isFoo
.markSafe
return type isString & { __html: string }
.The text was updated successfully, but these errors were encountered: