TypeScript Version: 3.1.0-dev.20180807
Search Terms:
- object spread own enumerable
- object spread accessor
- getter object spread
Code (playground)
class Phone {
get number() {
return 1234;
}
getNumber() {
return 1234;
}
}
const phoneInstance = new Phone();
const phoneClone = { ...phoneInstance };
Expected behavior:
The inferred type for phoneClone should be {}
Actual behavior:
The inferred type for phoneClone is { number: number }
Playground Link: playground
Related Issues:
Discussion
- I realize that spreading a class instance is not desirable.
- I also realize (after searching for dupes and reading similar issues for a couple hours) that there are design limitations around TS not tracking own/enumerable properties.
But, from what I understand, it makes a guess and correctly omits methods on classes from the inferred type of spreading an instance of that class. Since getters on classes are also on the prototype, it seems like inconsistent behavior that methods are omitted from the inferred type but get/set accessors are not. Even though the compiler doesn’t really track what properties are own enumerable, it seems like it could use similar heuristics to determine, in this simple case, that the getter won’t be present on the spread object.
P.S. – thanks for your time, and apologies in advance if this does turn out to be a dupe that I couldn’t find 😬
TypeScript Version: 3.1.0-dev.20180807
Search Terms:
Code (playground)
Expected behavior:
The inferred type for
phoneCloneshould be{}Actual behavior:
The inferred type for
phoneCloneis{ number: number }Playground Link: playground
Related Issues:
Discussion
But, from what I understand, it makes a guess and correctly omits methods on classes from the inferred type of spreading an instance of that class. Since getters on classes are also on the prototype, it seems like inconsistent behavior that methods are omitted from the inferred type but get/set accessors are not. Even though the compiler doesn’t really track what properties are own enumerable, it seems like it could use similar heuristics to determine, in this simple case, that the getter won’t be present on the spread object.
P.S. – thanks for your time, and apologies in advance if this does turn out to be a dupe that I couldn’t find 😬