Class's prototype property is typed incorrectly and includes instance properties
Closed this issue ยท 4 comments
๐ Search Terms
class prototype typeof instanceof
๐ Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about all Common "Bugs" That Aren't Bugs
โฏ Playground Link
๐ป Code
class A {
foo = 2;
bar() { return 1; }
}
console.log(A.prototype.foo, 'foo' in A.prototype);
console.log(A.prototype.bar, 'bar' in A.prototype);๐ Actual behavior
The type of A.prototype includes foo, which is an instance field and not actually present on the prototype.
๐ Expected behavior
The type of A.prototype should not include instance fields like foo.
Additional information about the issue
My motivation for looking into was trying to write a utility type that differentiated between instance properties and prototype properties in order to generate proper React props types from a class that ignored things that should not be set from the prototype. Right now there is no way at the type level to tell a field from a prototype method at all that I can see.
I believe this is essentially due to the lack of #9726.
I don't think this should be modelled as an enumerable properties issue. It's more that the instance type of a class is a union (or more accurately an extends type operation) of the instance properties with the prototype's type, but instead the prototype is typed the same as the instance type.
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.