Reputation:
I have the following code:
class Parent {
public static message: string;
}
class FirstChild extends Parent {
public static message: string = "Hello from first child";
}
class SecondChild extends Parent {
public static message: string = "Hello from second child";
}
const children: (new() => Parent) = [FirstChild, SecondChild];
console.log(children[0].message); // tsc error: property does not exist
I get this error:
property message does not exist on type new() => Parent
which makes sense, because the type is only referring to the constructor signature.
My question is: which type do I use to describe an array with constructor signatures of classes extending from a parent + the static attributes from this parent?
Upvotes: 1
Views: 408
Reputation: 187004
Anytime you want to reference a class object, and not an instance, you want to use:
typeof MyClass
That means that you want an array of typeof Parent
instead:
const children: (typeof Parent)[] = [FirstChild, SecondChild];
console.log(children[0].message); // string
Upvotes: 0