我想用一个变量保存类实例数组,元素类型要允许任何从某个“基类”派生的实例。这个类实例数组的例子能更清楚地说明我的意思。
class Parent { } //Common parent
class FirstChild extends Parent { }
class SecondChild extends Parent { }
//How should this declaration look?
let instances: <U extends Parent>U[] = []; //Allow all descedants of 'Parent' here
//Then these calls would work
instances.push(new Parent());
instances.push(new FirstChild());
instances.push(new SecondChild());
先谢谢大家。