You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It's possible to infer the this type contextually on a static method so inherited classes get their own type as this when calling an inherited static method. But in static property getters it's not possible to specify generic parameters, so it's not possible to get the this type of a derived class in a parent getter.
In the example below, Bar.default returns an instance of Bar but it's type is inferred as Foo.
typeConstructor<T>={new(): T}classFoo{staticfactory<TextendsFoo>(this: Constructor<T>): T{returnnewthis()}staticgetdefault(){// Not possible to specify type parametersreturnnewthis()}}classBarextendsFoo{}constx=Bar.factory()// x is type Barconsty=Bar.default// y is type Foo// however, both are instances of Barconsole.log(xinstanceofBar)// trueconsole.log(yinstanceofBar)// true
Ideally, I think both getter and method should work without any extra type parameters needing to be specified. Static this type should be inferred contextually.
classFoo{staticfactory(): this {// TS2526: A 'this' type is available only in a non-static member of a class or interface.returnnewthis()}staticgetdefault(): this {returnnewthis()}}
Edit: I'm seeing some comments in other threads about new this() not being allowed because the caller's ctor signature isn't known in the parent class. That's understandable. This is a demonstration of the problem of type inference, this is not an actual use case. My actual use cases don't invoke any unknown constructors. So please don't dismiss this because you think the use case isn't viable.
🔎 Search Terms
static property getter
static property this
The text was updated successfully, but these errors were encountered:
Bug Report
It's possible to infer the
this
type contextually on a static method so inherited classes get their own type asthis
when calling an inherited static method. But in static property getters it's not possible to specify generic parameters, so it's not possible to get thethis
type of a derived class in a parent getter.In the example below,
Bar.default
returns an instance of Bar but it's type is inferred as Foo.Ideally, I think both getter and method should work without any extra type parameters needing to be specified. Static
this
type should be inferred contextually.Edit: I'm seeing some comments in other threads about
new this()
not being allowed because the caller's ctor signature isn't known in the parent class. That's understandable. This is a demonstration of the problem of type inference, this is not an actual use case. My actual use cases don't invoke any unknown constructors. So please don't dismiss this because you think the use case isn't viable.🔎 Search Terms
The text was updated successfully, but these errors were encountered: