question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return types of Object.assign

See original GitHub issue

Bug 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.

  1. For createFoo above, the returned type specifies & { bar: number }, even though { bar: number } is already part of the Foo type.
  2. For markSafe above, the returned type specifies string, even though what is actually returned is a String object with the additional property added, not a primitive string (so, for example, markSafe('') !== markSafe('')).

🙂 Expected behavior

  1. createFoo return type is Foo.
  2. markSafe return type is String & { __html: string }.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fatcerberuscommented, Jul 15, 2021

HTMLCanvasElement & { width: number, height: number } should be functionally equivalent to HTMLCanvasElement in every way - if it’s not, then that sounds like a bug.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found