我採用了棱角分明4.在我的代碼,我想創建嵌套泛型類的實例如下面的代碼開發應用程序中創建嵌套泛型實例:爲打字稿
export class Page<T>
{
constructor(c: { new(): T; }){
this.FilterData = new c();
}
TotalItems: number;
PageSize: number;
CurrentPage: number;
TotalPage: number;
FilterData: T;
}
export class Filter<T>
{
constructor(c: { new(): T; })
{
this.AdvanceData = new c();
}
AdvanceData: T
IsAdvance: boolean;
OrderMeThod: string;
OrderBy: string;
KeyWord: string;
}
export class Client
{
Name:string;
}
而且我不知道我該如何初始化Page<Filter<Client>>
的對象,其中Filter
和Client
是在構造函數中初始化的。我試過了,例如:new Page<Filter<Client>>();
但它沒有工作。 請幫我解決這個問題。謝謝
您不能。類型系統在運行時不存在,因此通用約束也不存在。爲什麼不將實例傳遞給不同的構造函數?是否有理由通過課程? –
感謝您的評論。我的問題通過以下答案解決。 –