【发布时间】:2019-01-01 13:44:51
【问题描述】:
我正在尝试编写一个受保护的抽象类,它可以将子类类型作为超类构造函数的方法签名中的类型参数。
我正在寻找类似于 C# 的通用类型约束(where 关键字),以便我可以在参数列表中使用子类型。
// where T : <base class name>
BaseAuthController<T> where T : BaseAuthController
当前超类
export abstract class BaseAuthController {
protected constructor(
protected dialogRef:
//This class shouldn't know about child classes
MatDialogRef<Child1DialogComponent> |
MatDialogRef<Child2DialogComponent> |
MatDialogRef<Child3DialogComponent>
) {
}
}
当前子类
export class Child1DialogComponent extends BaseAuthController {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
理想的超类
export abstract class BaseAuthController<T> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
参考文献
【问题讨论】:
标签: c# angular typescript generics typescript-generics