Angular error - Generic type 'ModuleWithProviders<T>' requires 1 type argument(s)
AngularAngular Problem Overview
After upgrading from Angular version 8 to 10.
Running the - ng serve command gives me error -
ERROR in node_modules/ngx-tree-select/src/module.d.ts:11:56 - error TS2314: Generic type 'ModuleWithProviders
11 static forRoot(options: TreeSelectDefaultOptions): ModuleWithProviders; ~~~~~~~~~~~~~~~~~~~
This is my file - fronent/webapp/node_modules/ngx-tree-select/src/module.d.ts
import { ModuleWithProviders } from '@angular/core';
import { TreeSelectDefaultOptions } from './models/tree-select-default-options';
import * as ɵngcc0 from '@angular/core';
import * as ɵngcc1 from './components/tree-select.component';
import * as ɵngcc2 from './components/tree-select-item.component';
import * as ɵngcc3 from './directives/off-click.directive';
import * as ɵngcc4 from './pipes/item.pipe';
import * as ɵngcc5 from '@angular/common';
import * as ɵngcc6 from '@angular/forms';
export declare class NgxTreeSelectModule {
static forRoot(options: TreeSelectDefaultOptions): ModuleWithProviders;
static ɵmod: ɵngcc0.ɵɵNgModuleDefWithMeta<NgxTreeSelectModule, [typeof ɵngcc1.TreeSelectComponent, typeof ɵngcc2.TreeSelectItemComponent, typeof ɵngcc3.OffClickDirective, typeof ɵngcc4.ItemPipe], [typeof ɵngcc5.CommonModule, typeof ɵngcc6.FormsModule], [typeof ɵngcc1.TreeSelectComponent]>;
static ɵinj: ɵngcc0.ɵɵInjectorDef<NgxTreeSelectModule>;
}
//# sourceMappingURL=module.d.ts.map
Kindly view the image for the error.
Angular Solutions
Solution 1 - Angular
@user9882419 has the best approach i believe. Here is an example to illustrate what he is saying
export class AppSharedModule {
static forRoot(): ModuleWithProviders<AppSharedModule> {
return {
ngModule: AppSharedModule
};
}
}
Solution 2 - Angular
To skip this type error just add in you code:
declare module "@angular/core" {
interface ModuleWithProviders<T = any> {
ngModule: Type<T>;
providers?: Provider[];
}
}
Note: this will fix the type checking and allow to continue - if you will notice other lib to Angular10 inconmpatibilities - you need to ask lib upgrade or found another one.
Solution 3 - Angular
Solution 4 - Angular
The solve for the same error in my Angular 10.1.3 version, was change the param of export to
With error:
export const appRoutingProviders: any[] = [];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
After change:
export const appRoutingProviders: any[] = [];
export const routing: ModuleWithProviders<any> = RouterModule.forRoot(appRoutes);
Solution 5 - Angular
You just need to specify the type (the name of the module class) between ModuleWithProviders
Solution 6 - Angular
One cause of this is upgrading Angular to a newer version (i.e. v10), yet having an older JS library that doesn't play nice and also needs to be updated. In my case I received the following error like the OP:
> ERROR in [at-loader]
> ./node_modules/@ng-bootstrap/ng-bootstrap/popover/popover.module.d.ts:6:23
> TS2314: Generic type 'ModuleWithProviders
Notice in my instance the particular offending library was called out which was @ng-bootstrap
It indeed was on a much older version:
"@ng-bootstrap/ng-bootstrap": "^2.0.0"
I was able to update the package to version 7.0.0
and the error went away and the application built successfully. The main solution here is don't immediately throw in the fixes recommended here or use <any>
in place of a type. There might just be another package you need to update to bring you app compliant with the version of Angular being used to fix the issue correctly.
Solution 7 - Angular
It seems this ngx-tree-select library has not evolved with latest Angular versions. Maybe you should open a PR request or an issue on the repo.
Solution 8 - Angular
Just provide "unknown" as Type-Argument. I had the same issue and I solved it as in the following snippet
export const routingModule: ModuleWithProviders<unknown> = RouterModule.forRoot(
routes
);
Solution 9 - Angular
since you got error in node modules you have limited scope to fix. So please try to get the latest compatible version with the angular(your version).
Solution 10 - Angular
I had a bunch of errors. I had to go through them one by one and update the packages to later versions where the publishers had already fixed this. Look at the file name and it'll give you a clue what package needs to be updated. At least one of the packages didn't have a current update and had a massive backlog of issues and pull requests. I don't know what I'm going to do about that one. I might either have to replace that package with something else or use one of the solutions here.
Solution 11 - Angular
If you are facing issues in toastr.module.ts
Before:-
export declare class ToastrModule {
static forRoot(config?: Partial<GlobalConfig>): ModuleWithProviders;
}
export declare class ToastrComponentlessModule {
static forRoot(config?: Partial<GlobalConfig>): ModuleWithProviders;
}
After:-
export declare class ToastrModule {
static forRoot(config?: Partial<GlobalConfig>): ModuleWithProviders<any>;
}
export declare class ToastrComponentlessModule {
static forRoot(config?: Partial<GlobalConfig>): ModuleWithProviders<any>;
}