I'm currently working on implementing a factory method that can return classes dynamically, and here's my code snippet:
getWidget<T extends WidgetBase>(componentName: string): Type<T> {
switch (componentName) {
default:
throw new Error(`Widget ${componentName} is not registered in the system`);
case "Widget2":
return Widget2Component;
}
}
In this code block, Widget2Component is a class defined in another file as an Angular component that inherits from WidgetBase, which is an abstract class. Additionally, Type<T> is an Angular interface structured like so:
interface Type<T> extends Function {
new (...args: any[]): T
}
After compiling, I encountered the following error message:
Type 'typeof Widget2Component' is not assignable to type 'Type<T>'
.
Type 'Widget2Component' is not assignable to type 'T'
.
This issue has been puzzling me because Widget2Component does indeed extend WidgetBase!