【发布时间】:2021-02-06 09:58:59
【问题描述】:
我有一个导出的抽象类,目前有一个泛型。然而,我现在需要两个泛型。我不想更改当前使用此类的所有切除类。所以我想添加一个扩展接口的可选泛型类。
这是我目前拥有的
export abstract class SharedShell<T extends IBase, T1 extends IBase> implements OnInit, OnDestroy {}
使 T1 成为可选的最佳方法是什么?我尝试执行以下操作
export abstract class SharedShell<T extends IBase, T1 extends IBase | Undefined = Undefined> implements OnInit, OnDestroy {}
但是这会导致类型错误。
'IBase' is assignable to the constraint of type 'T1', but 'T1' could be instantiated with a different subtype of constraint 'IBase'.
这就是我卡住的地方,我怎样才能最好地解决这个问题?
【问题讨论】:
-
您能否提供一个完整的可重现示例,即使用 TS 游乐场?从您的代码中我看不到错误:
-
@mbdavis 我在原帖中添加了一个示例。
-
谢谢!所以澄清一下 - 当提供
T1时,您希望 detailService 成为ISharedService<T | T1>,否则只是ISharedService<T>? -
@mbdavis 是的,没错。
标签: angular typescript generics