我有以下代码:
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
我得到这个错误:
类型new((上不存在属性消息=>父
这很有意义,因为类型只引用构造函数签名。
我的问题是:我应该使用哪种类型来描述一个数组,该数组具有从父级扩展的类的构造函数签名+该父级的静态属性?
任何时候,只要你想引用一个类对象,而不是一个实例,你都想使用:
typeof MyClass
这意味着您需要一个typeof Parent
数组:
const children: (typeof Parent)[] = [FirstChild, SecondChild];
console.log(children[0].message); // string
游乐场