Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class's prototype property is typed incorrectly and includes instance properties #55904

Closed
justinfagnani opened this issue Sep 28, 2023 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@justinfagnani
Copy link

πŸ”Ž 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

https://www.typescriptlang.org/play?target=9#code/MYGwhgzhAECC0G8BQ1oDMD2HoF5oCYBuFaAIzACcAKASkWgoFMAXAVwoDtoBGQ6AXySCkwDBwgYQjAHQgMAcyqxpABwoZmGgJ4qZmDABpoAcn3HoASy7K1G7bprFR4yTLmKb6zcx0zyFI2N-cys4VS97RkckIA

πŸ’» 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.

@justinfagnani justinfagnani changed the title Class's prototype property is typed incorrectly Class's prototype property is typed incorrectly and includes instance properties Sep 28, 2023
@MartinJohns
Copy link
Contributor

I believe this is essentially due to the lack of #9726.

@justinfagnani
Copy link
Author

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.

@jcalz
Copy link
Contributor

jcalz commented Sep 28, 2023

Duplicate #11558 #20922 #30516 #41738

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 2, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants