Warm tip: This article is reproduced from serverfault.com, please click

Angular 10 Swagger Codegen: Generic type ModuleWithProviders<T> requires 1 type argument(s)

发布于 2020-07-30 04:48:39

I am generating https://editor.swagger.io/ Codegen proxies. It is giving the following error in Angular 10. How can this be fixed?

Generic type 'ModuleWithProviders' requires 1 type argument(s).

export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        };
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule,
                 @Optional() http: HttpClient) {
        if (parentModule) {
            throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
        }
        if (!http) {
            throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
            'See also https://github.com/angular/angular/issues/20575');
        }
    }
}
Questioner
user13889515
Viewed
0
Pavel B. 2020-07-30 13:14:39

this error tells you that, the ModuleWithProviders class has 1 generic paramter. so you sould use it like ModuleWithProviders<T> where T is a type.

EDIT:

The class is defined like this:

interface ModuleWithProviders<T> {
  ngModule: Type<T>
  providers?: Provider[]
}

.

export class ApiModule {
  public static forRoot(configurationFactory: () => Configuration) : ModuleWithProviders<ApiModule> {
    return {
        ngModule: ApiModule,
        providers: [ { provide: Configuration, useFactory: configurationFactory } ]
    };
}

See Resource:

https://angular.io/guide/migration-module-with-providers