I've hit a roadblock in my Typescript project.
Essentially, I am trying to invoke a static function from a class that extends a specific abstract class.
However, an error is being thrown:
The 'this' context of type 'typeof A' is not assignable to method's 'this' of type 'AStatic<AStatic<unknown>>'. Cannot assign an abstract constructor type to a non-abstract constructor type.
For reference, here is the Typescript playground.
Below is the snippet of code exhibiting this issue:
type AStatic<S extends A> = { new(): S };
abstract class A {
static callStatic<S extends AStatic<S>>(this: AStatic<S>) {
console.log('hey')
}
}
class B extends A {
}
class D extends A {
}
class C {
aType: typeof A;
constructor(type: typeof A) {
this.aType = type;
this.aType.callStatic(); // The error occurs at this line
}
}
const c = new C(B);
const c_2 = new C(D);
To bypass this error in Typescript, I have resorted to using `any` instead of `typeof A`, but this means sacrificing IDE support for A's functions.
Note that I do not have control over class A and type AStatic as they are part of an external library.