I'm looking for a way to express the type of [B, C] from the following code in a generic way. If I hover types as it currently is I get const types: (typeof B | typeof C)[] which is a bit verbose and could get very long as new items are added.
abstract class A {
static staticF<T extends A>(this: new () => T): string {
return 'A'
}
}
class B extends A {
static bProp = 1
}
class C extends A {
static cProp = 1
static staticF<T extends A>(this: new () => T): string {
return 'B'
}
}
const types = [B, C]
types
.map(t => t.staticF())
.forEach(x => console.log(x))
I tried using const types: typeof A[] but I get the following error:
The 'this' context of type 'typeof A' is not assignable to method's 'this' of type 'new () => A'. Cannot assign an abstract constructor type to a non-abstract constructor type.
I also tried const types: typeof extends A[] but TS thinks I'm drunk.
How can I express the types of multiple classes constructors from classes which share the same parent?
Also, what is the difference between typeof A, new () => A and {new (): A}?