To create dynamic components, I utilize the following method:
public addItem<T extends WidgetComponent>(ngItem: {new(): T}): T {
let factory = this._componentFactoryResolver.resolveComponentFactory(ngItem);
const ref = this._viewCntRef.createComponent(factory);
const newItem: T = ref.instance as T;
...
return newItem;
}
The method can be called in this manner:
const ref: MyWidgetComponent = this.dashboard.addItem<MyWidgetComponent>(MyWidgetComponent);
However, TypeScript is showing a compilation error:
app.component.ts:45:35
Untyped function calls may not accept type arguments.
I attempted to replace {new(): T}
with Type<T>
, but the same error persists:
app.component.ts:45:35
Untyped function calls may not accept type arguments.
What would be the correct definition in this scenario? Despite the compilation error, the code functions perfectly...
EDIT: For those interested, here is the complete code snippet https://github.com/jaumard/ng2-dashboard/blob/master/components/dashboard/dashboard.component.ts#L99