Return types of Object.assign
See original GitHub issueBug Report
🔎 Search Terms
Object.assign
🕗 Version & Regression Information
4.3.2
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about
Object.assign
⏯ Playground Link
Playground link with relevant code
💻 Code
type Foo = { bar: number }
// expected return type: Foo
// actual return type: Foo & { bar: number }
const createFoo = (obj: Foo) =>
Object.assign(obj, {
bar: 1,
})
// expected return type: String & { __html: string }
// actual return type: string & { __html: string }
const markSafe = (html: string) =>
Object.assign(html, {
__html: html,
})
🙁 Actual behavior
This may be 2 separate bugs, but I suspect they may be related.
- For
createFoo
above, the returned type specifies& { bar: number }
, even though{ bar: number }
is already part of theFoo
type. - For
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 }
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Object.assign() - JavaScript - MDN Web Docs
The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the ...
Read more >React typescript Object.assign return type - Stack Overflow
I have this small custom hook which returns an Object. assign result. In documentation it says that this returns an array, but the...
Read more >JavaScript Object.assign() Method - GeeksforGeeks
The Object.assign() method is used to copy the values and properties from one or more source objects to a target object.
Read more >Class.assign() to return an typed Object.assign() #11508
Scenario: Right now Object.assign() doesn't return a typed Object. ... So the returned type would be the original type to assign. Right?
Read more >Documentation - Object Types - TypeScript
It is possible to support both types of indexers, but the type returned from a numeric indexer must be a subtype of the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
HTMLCanvasElement & { width: number, height: number }
should be functionally equivalent toHTMLCanvasElement
in every way - if it’s not, then that sounds like a bug.I think
assign<T, U>(target: T, source: U): T & U;
signature is wrong. Probably it should beassign<T, U>(a: T, b: U): Omit<T, keyof U> & U;
https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhjAvDA3gDwFwwIwF8DcAUKJLAEZKqYwBEAZiCDboceNDMJQPJkBWAU2BQAdHAgQAlgHMwACjgAaGGQCU+GAHpNnGJIhUsYAQDcBAJxaE4ItCKggAylHOSw0uesJlb9py7cPL2BfB2dXd08iQgATIQAbOHMBGDoAVzBhSXB4CRkwAB4AFWUAVQA+BSwSlSxS1SxuAFtJKGLlAGsBAE8QOhgKmAAyAeiSDhjKcSlZBWU1DW0YSf1DGGgI6RYY0P9NqNjfeIF3KAALfCA