I'm working on creating a protected abstract class that allows for the subclass type to be passed as a type argument in the constructor of the superclass.
What I need is something similar to C#'s Generic Type Constraint (using the where
keyword) so that I can specify a child type in the parameter list.
// where T : <base class name>
BaseAuthController<T> where T : BaseAuthController
Existing superclass
export abstract class BaseAuthController {
protected constructor(
protected dialogRef:
//This class should not have knowledge of child classes
MatDialogRef<Child1DialogComponent> |
MatDialogRef<Child2DialogComponent> |
MatDialogRef<Child3DialogComponent>
) {
}
}
Existing subclass
export class Child1DialogComponent extends BaseAuthController {
constructor(dialogRef: MatDialogRef<Child1DialogComponent>) {
super(dialogRef);
}
}
Preferred superclass structure
export abstract class BaseAuthController<T> {
protected constructor(protected dialogRef: MatDialogRef<T>) {
}
}
References
- C# Generic Type Constraint Reference
- TypeScript Generics Reference
- Possibly related SO Post