我想要一個變量來存儲一個類實例數組,但我想指定數據類型作爲從「基」類繼承的任何東西。這更好地通過示例演示: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());
在此先感謝。
如何:'我們例子= []作爲媽媽[];'? –