Javascript: Object.assign to assign property values for classes is not respected
See original GitHub issuePerhaps TS should be able to identify Object.assign(this, ...)
then infer the properties that are assigned to this
? Or is this not possible to do? I am not sure.
TypeScript Version:
Version 3.0.3
Search Terms:
- object assign
- class
- does not exist on type
Code A.js
export default class A {
constructor ({ x, y, z }) {
Object.assign(this, {x, y, z});
}
f () {
return this.x;
}
}
Expected behavior:
TS should be able to identify x
as a property of this
.
Actual behavior: Throwing:
Property 'x' does not exist on type 'A'.
Related Issues: no
Side Note: I am using TS with Javascript because YouCompleteMe switched to using TSServer for semantic completion. I absolutely love TS so was really happy about the switch! Thanks for the great work!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:56
- Comments:16 (1 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.
Read more >object.assign does not copy functions [duplicate]
As I mentioned in my answer, the properties that are not being copied (which you say contain functions), are probably on the prototype...
Read more >14. New OOP features besides classes - Exploring JS
Only enumerable own properties: Object.assign() ignores inherited properties and properties that are not enumerable. Reading values: the values are read ...
Read more >JavaScript Object.assign() - Programiz
The JavaScript Object.assign() method copies all enumerable own properties of given objects to a single object and returns it. In this article, you...
Read more >JavaScript Object.assign() Method - GeeksforGeeks
Javascript · A TypeError is raised if the property is non-writable. · The target object can be changed only if the properties are...
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
Came here from #28883. I understand that in the general case, the type of the second argument to
Object.assign
could be of typeany
, but in my specific case I define that type via an interface beforehand:It seems to me that comparison between the types of
this
andconfig
could be done, and ifconfig
is some type that’s not assignable tothis
, then that would be the condition where you couldn’t determine the type.The alternative right now is to manually assign every property of the config interface to the corresponding class property, which works but is a pain:
(This whole thing gets even more ugly when dealing when using optional properties on the config object to allow the use of default values:
and as you can see, with lots of properties on the config object, you quickly wind up with a large chunk of code that just does the same job
Object.assign
normally does, as it automatically handles optional properties. However, becauseObject.assign
interprets a key with valueundefined
as present, this would be blocked on #13195 for full compatibility.)Is there any way to achieve this right now? If not, is it viable to implement? I could look into putting a PR together, but no promises as I’ve never worked with TS’s source before.
Why not support return different thing from constructor()?
It’s more common than only Object.assign.