我想要一个变量来存储类实例数组,但是我想将数据类型指定为任何继承自“基”类的数据。通过示例可以更好地说明这一点:

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());

提前致谢。

最佳答案

试试这个

let example : Array<Mom> = [ ];

参见 here

09-25 19:37