Skip to content

Incomplete types for InjectionToken #381

@dsfcoll

Description

@dsfcoll

Thank you for awesome library!

I found that types has an edge case that is not covered.

There is no way to pass optional injection token.

In nest js there is a syntax for optional injection:

const connectionProvider = {
  provide: 'CONNECTION',
  useFactory: (optionsProvider: MyOptionsProvider, optionalProvider?: string) => {
    const options = optionsProvider.get();
    return new DatabaseConnection(options);
  },
  inject: [MyOptionsProvider, { token: 'SomeOptionalProvider', optional: true }],
  //       \______________/             \__________________/
  //        This provider                The provider with this token
  //        is mandatory.                can resolve to `undefined`.
};

https://docs.nestjs.com/fundamentals/custom-providers

But this syntax is not described in types ClsModule.forFeatureAsync

So in my example:

ClsModule.forFeatureAsync({
  provide: getTenantDataSourceToken(moduleName),
  strict: true,
  inject: [
    CLS_REQ,
    { token: getMetricsServiceToken(moduleName), optional: true },
     ^^^^^
  ],
  ....

I get an error: Object literal may only specify known properties, and token does not exist in type Function | Type<any> | Abstract<any

Metadata

Metadata

Assignees

No one assigned

    Labels

    contribution welcomeThis is a good candidate for a community contributionenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions