在 Typescript 中使用泛型创建 Array<T> 时未解析的类型 T

Unresolved type T when creating an Array<T> with generic in Typescript

所以我正在尝试创建一个包含一系列项目的对象我希望项目是通用的但是我收到错误 unresolved type T:

export class AllocationInvestorVO {
    public name: string;
    public id: string;
    public projects: Array<T>; //ERROR: unresolved type T
}

我的目标是我们在 Java 中看到的多态行为,我们可以创建一个可以扩展到 ArrayList 的 List 对象,例如:

如何创建一个 Array,其基本类型为 Project,它可以变形为 ProjectXProjectY.[=17= 类型的数组]

如果您希望 projects 是通用的,那么您需要先将 AllocationInvestorVO class 声明为通用的:

export class AllocationInvestorVO<T> {
    public name: string;
    public id: string;
    public projects: Array<T>; // ok
}

否则T无法解析
如果您想为此 T 建立基础,那么:

export class AllocationInvestorVO<T extends Project> {
    public name: string;
    public id: string;
    public projects: Array<T>;
}