-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Suggestion: Improve type of constructor
on the instance type of a class
#32452
Comments
That sounds great; I'm not concerned with actually constructing or calling the constructor; just about using |
edit: I was being stupid, ignore this. |
@fatcerberus It's very much writable and configurable, everywhere. |
Hmm, you're right:
I could have sworn I remembered it being nonconfigurable. I guess I was thinking of the prototype itself:
My mistake, carry on 👍 |
This will definitely improve the quality of life of those who use static class methods. Currently one have to either cast the What is the point of having the static methods, which you can't (according to the type-checker) call from instances? |
As always there are lengthy discussions about the simple, quality of life proposal: for every class:
define the type of
Can we have this simple improvement? |
This could be easily done with a macros. Then people would start using it right now, finding corner cases, making evaluation. At some point it would be clear if its good change for everyone or its someones, very specific requirement. In the 1st case this "macros" could be included in the TS core, in the 2nd - nobody cares and TS team does not have to care as well. But TS team does not want to support macroses out of the box to allow language experiments in the user space. Instead they want to proceed through lengthy discussions, which lasts for years. |
I've encountered problems with accessing static private fields class Test {
static #staticPrivateField = 'test' ; // (1) #staticPrivateField' is declared but its value is never read.ts(6133)
constructor() {
console.log(
this.constructor.#staticPrivateField // (2) Property '#staticPrivateField' does not exist on type 'Function'.ts(2339)
)
}
}
new Test(); note: (1) is a warning that shows up in vscode even if the file is .js, which is pretty annoying Please fix? 😬 |
Keep in mind this example wouldn't work if you subclass |
I would also be interested in this so i can generically get the static version without type TypeofClass<T extends { constructor: new (...args: any[]) => any }> = T extends { constructor: infer S } ? S : never; |
Another vote for typing |
Search Terms
constructor type
Suggestion
This was previously discussed in this thread: DefinitelyTyped/DefinitelyTyped#36660 (comment)
Currently, the type of the
constructor
property on most objects isFunction
. It has been suggested that for anyclass C {}
, the type of theconstructor
property on the instance should betypeof C
. However this suffers a significant drawback in that it severely limits subclasses as any subclass ofC
must have the same (or compatible) construct signatures asC
.Here is an example of this issue: https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEDKCuAHApgJwMLitA3gKGmjAC5oIAXVASwDsBzAbgOmAHsaLV5hzXUAKEmUq06ASlzNC5ABZUIAOjDQAvESaEAvnm15QkGAgBGmA9GQAPcshoATQ0jSns+QkdKdRGlu07deAqyI5BCkOEQeIvQANNDuwtT00JoSroRkjoHBimBi3tJyikaq0EEhCkbe2rq01qgAZmDAyHCZzjBphGwclP58pOQAniis9a0oGFgQTDU0dY3NrSZTkuk+PVw8-dBDI2PG7TNAA
Instead, I would suggest a mechanism to type
constructor
as all of the static members oftypeof C
but none of the call/construct signatures oftypeof C
, yet still having an apparent type ofFunction
.Use Cases
In @ljharb's
qs
, he'd like to be able to use theconstructor
property of aBuffer
-like object to access theisBuffer
method on the constructor in a type-safe way (i.e.obj.constructor.isBuffer
).Examples
Workaround
There exists a possible workaround for this currently, though it is somewhat complicated:
Playground link: https://www.typescriptlang.org/play/index.html#code/MYGwhgzhAEDKCuAHApgJwMLitA3gKGmjAC5oIAXVASwDsBzAbgOmAHsaLV5hzXUAKEmUq06ASlzNC5ABZUIAOjDQAvESaEAvnm15QkGAgBGmA9GQAPcshoATQ0jSns+QkdKdRGlu07deAqyI5BCkOEQeIvQANNDuwtT00JoSroRkjoHBimBi3tJyikaq0EEhCkbe2rrkAJ4ocORg5FTAALLIALZGaBAAPAAqzjCW1nYwAGLwNDxU7AB8JQAKrQDWg8Oxq8i1rABm0ENYEIsAZNBTMy3sTHi01qh7YMDIcJnDkuk+HJT+fKSwJotdpdHqofp1FD7N4oDDHeZMXT3NBPF5vEzHT7pNg-Lg8f6NZqtDrdXp9SHIaHGYYInRAA
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: