我有以下(相当复杂的)扩展链。这是我的问题的一个可重复性最小的例子(显然,我的应用程序中的类将具有行为实现)。
export class BaseModel
{
public someBaseModelMethod(): void { }
}
export class BaseView
{
public someBaseViewMethod(): void { }
}
export class CollectionView<TChildView extends BaseView, TModel extends BaseModel>
extends BaseView
{
public someCollectionViewMethod(): void { }
}
export class ItemView<TModel extends BaseModel>
extends BaseView
{
public setItem(model: TModel): void { }
}
export interface IGridView<TChildView extends ItemView<TModel>, TModel extends BaseModel>
extends CollectionView<TChildView, TModel>
{
someGridViewMethod(): void;
}
export class GridView<TChildView extends ItemView<TModel>, TModel extends BaseModel>
extends CollectionView<TChildView, TModel>
implements IGridView<TChildView, TModel>
{
public someGridViewMethod(): void { }
}
function bind<TInterface>(key: string, implementation: new (...args: any[]) => TInterface) {
// do some bindings here
}
bind<IGridView<ItemView<BaseModel>, BaseModel>>("IGridView", GridView);
编译时,我在最后一行的GridView
部分(bind
语句)出错。错误如下:
TS2345 Argument of type 'typeof GridView' is not assignable to parameter of type 'new (...args: any[]) => IGridView<ItemView<BaseModel>, BaseModel>'.
Type 'CollectionView<any, any>' is not assignable to type 'IGridView<ItemView<BaseModel>, BaseModel>'.
Property 'someGridViewMethod' is missing in type 'CollectionView<any, any>'.
正如您所看到的那样,someGridViewMethod
期待CollectionView
。但是,GridView
延伸 CollectionView
,为什么期望在GridView
类上找到CollectionView
方法?
代码在2.0.3
中正确编译,但在1.8.10
中没有。
答案 0 :(得分:1)
这适用于1.8.x
和2.x
:
type TypedGridView = { new (...args: any[]): IGridView<ItemView<BaseModel>, BaseModel> };
function bind<TInterface>(key: string, implementation: new (...args: any[]) => TInterface) {
// do some bindings here
}
bind("IGridView", GridView as TypedGridView);
似乎1.8.x
编译器无法识别GridView
作为构造函数:
IGridView<ItemView<BaseModel>, BaseModel>