Search code examples
angulartypescriptswaggerswagger-codegenangular10

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


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');
        }
    }
}

Solution

  • 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