Angular 16- how to avoid @optional

26 Views Asked by At

In my services, I have a constructor as

constructor(protected httpClient: HttpClient, @Optional()@Inject(BASE_PATH) basePath: string|string[], @Optional() configuration: Configuration){}

And in app.module I'm configuring it like

{
      provide: MyLoadingService,
      useFactory: (httpClient: HttpClient) =>
      new MyLoadingService(
        httpClient,
        'http://127.0.0.1:1001',
        new Configuration() // changes here
      ),
      deps: [HttpClient],
    }

In the constructor 2nd and 3rd parameters are optional, I don't want to pass the 3 parameters. Because I have many services and their configuration class are unique to each one. So I don't want to complicate the import statement. If I pass null, undefined, or no 3rd parameter in the factory, the compiler is throwing an error. So is there any way to avoid sending optional parameters? I'm using Angular 16 with strict mode.

0

There are 0 best solutions below