TypeScript:等同于扩展 class 的 C# 通用类型约束?
TypeScript: Equivalent of C#'s Generic Type Constraint for extending class?
我正在尝试编写一个受保护的抽象 class,它可以将 subclass 类型作为 superclass 构造函数方法签名中的类型参数。
我正在寻找的类似于 C# 的通用类型约束(where
关键字),以便我可以在参数列表中使用子类型。
// where T : <base class name>
BaseAuthController<T> where T : BaseAuthController
当前超class
export abstract class BaseAuthController {
protected constructor(
protected dialogRef:
//This class shouldn't know about child classes
MatDialogRef<Child1DialogComponent> |
MatDialogRef<Child2DialogComponent> |
MatDialogRef<Child3DialogComponent>
) {
}
}
当前子class
export class Child1DialogComponent extends BaseAuthController {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
理想超class
export abstract class BaseAuthController<T> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
参考资料
- C# Generic Type Constraint Reference
- TypeScript Generics Reference
- Possibly related SO Post
当然在操作上有根本的不同,但是有一种方法可以达到相同的结果:
export abstract class BaseAuthController<T extends SubClass> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
结合同音类型意味着我们可以指定多个 children:
export abstract class BaseAuthController<T extends SubClass1 | SubClass2> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
我认为您可能需要自限泛型:
export abstract class BaseAuthController<T extends BaseAuthController<T>> {
protected constructor(protected dialogRef: MatDialogRef<T>) {}
}
此行为通常使用 polymorphic this
types, but you can't refer to the this
type in the constructor. There is an open issue about this but it doesn't look like it will be solved soon. Luckily, you can still just do it the way Java does 在 TypeScript 中完成。你的子类应该可以正常工作:
export class Child1DialogComponent extends BaseAuthController<Child1DialogComponent> {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
希望对您有所帮助;祝你好运!
我正在尝试编写一个受保护的抽象 class,它可以将 subclass 类型作为 superclass 构造函数方法签名中的类型参数。
我正在寻找的类似于 C# 的通用类型约束(where
关键字),以便我可以在参数列表中使用子类型。
// where T : <base class name>
BaseAuthController<T> where T : BaseAuthController
当前超class
export abstract class BaseAuthController {
protected constructor(
protected dialogRef:
//This class shouldn't know about child classes
MatDialogRef<Child1DialogComponent> |
MatDialogRef<Child2DialogComponent> |
MatDialogRef<Child3DialogComponent>
) {
}
}
当前子class
export class Child1DialogComponent extends BaseAuthController {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
理想超class
export abstract class BaseAuthController<T> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
参考资料
- C# Generic Type Constraint Reference
- TypeScript Generics Reference
- Possibly related SO Post
当然在操作上有根本的不同,但是有一种方法可以达到相同的结果:
export abstract class BaseAuthController<T extends SubClass> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
结合同音类型意味着我们可以指定多个 children:
export abstract class BaseAuthController<T extends SubClass1 | SubClass2> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
我认为您可能需要自限泛型:
export abstract class BaseAuthController<T extends BaseAuthController<T>> {
protected constructor(protected dialogRef: MatDialogRef<T>) {}
}
此行为通常使用 polymorphic this
types, but you can't refer to the this
type in the constructor. There is an open issue about this but it doesn't look like it will be solved soon. Luckily, you can still just do it the way Java does 在 TypeScript 中完成。你的子类应该可以正常工作:
export class Child1DialogComponent extends BaseAuthController<Child1DialogComponent> {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
希望对您有所帮助;祝你好运!