microsoft / microsoft/TypeScript
Javascript: Object.assign to assign property values for classes is not respected
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Perhaps 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!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and examine how the class constructor's Object.assign(this, {x, y, z}) is checked. Determine the expected type-checking behavior for this.x and add a regression test; done means the reported property error is resolved with coverage for this pattern.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100