我想要一个变量来存储一个类实例数组,但我想指定数据类型作为从“基”类继承的任何东西。这更好地通过示例演示:TypeScript变量,它包含一个扩展基类的类实例
class Mom { } //Base class
class Son extends Mom { }
class Daughter extends Mom { }
//What is the syntax for this?
let example : <T extends Mom>T[] = [ ]; //This array should store anyting that extends 'Mom'
//The this would be possible
example.push(new Mom());
example.push(new Son());
example.push(new Daughter());
在此先感谢。
如何:'我们例子= []作为妈妈[];'? –