基于Angular 7中的另一个泛型类实现泛型类
我在TypeScript中有以下接口和类:基于Angular 7中的另一个泛型类实现泛型类,angular,typescript,class,generics,interface,Angular,Typescript,Class,Generics,Interface,我在TypeScript中有以下接口和类: export interface PageInterface<T> { count: number; previous: string; next: string; results: T[]; } export class Page<T> implements PageInterface<T> {} -----------------------------------------
export interface PageInterface<T> {
count: number;
previous: string;
next: string;
results: T[];
}
export class Page<T> implements PageInterface<T> {}
----------------------------------------------------------
export interface AdapterInterface<T> {
adapt(item: any): T;
}
导出接口页面接口{
计数:数字;
上一个:字符串;
下一步:字符串;
结果:T[];
}
导出类页面实现页面接口{}
----------------------------------------------------------
导出接口适配器接口{
适应(项目:任何):T;
}
我需要实现PageAdapter
可能吗?我尝试了以下操作,但最终出现错误(键入“Page”不是泛型):
导出类PageAdapter实现AdapterInterface{
调整(项目:任何):第页{
返回新页面(项目['count']、项目['previous']、项目['next']、项目['results']);
}
}
如果我把Page
而不是Page
放在第一行,它根本不起作用
我如何实现这一点
非常感谢,我猜您正在寻找这样的产品:
export class PageAdapter<T> implements AdapterInterface<Page<T>> {
adapt(item: any): Page<T> {
return new Page<T>(item['count'], item['previous'], item['next'], item['results']);
}
}
export class PageAdapter2<P extends Page<any>> implements AdapterInterface<P> {
adapt(item: any): P {
return new Page(item['count'], item['previous'], item['next'], item['results']) as P;
}
}
这里,PageAdapter
可以用作适配器接口
。然而,这个版本的打字没有那么干净,需要实现
希望有帮助;祝你好运
第一段代码完成了我需要的内容。非常感谢你!
export class PageAdapter2<P extends Page<any>> implements AdapterInterface<P> {
adapt(item: any): P {
return new Page(item['count'], item['previous'], item['next'], item['results']) as P;
}
}