You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeSomeData={a: number};constgetSomeData=(): SomeData|undefined=>{returnundefined;}// expected data type: SomeData | undefined// actual data type: SomeDataconstdata=Object.assign({},getSomeData())
π Actual behavior
Incorrect type inference
π Expected behavior
Correct type inference
The text was updated successfully, but these errors were encountered:
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>.
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
π Actual behavior
Incorrect type inference
π Expected behavior
Correct type inference
The text was updated successfully, but these errors were encountered: