Skip to content

Opts

@tsed/di

Usage

typescript
import { Opts } from "@tsed/di";

See /packages/di/src/types/common/decorators/opts.ts.

Overview

ts
function Opts(target: any, propertyKey: string | symbol | undefined, index: number): void;

Description

Get instance options. This options depending on his invocation context.

typescript
import {Injectable, Opts, UseOpts} from "@tsed/di";

@Injectable()
class MyConfigurableService {
  source: string;
  constructor(@Opts options: any = {}) {
     console.log("Hello ", options.source); // log: Hello Service1 then Hello Service2

     this.source = options.source;
  }
}

@Injectable()
class MyService1 {
  constructor(@UseOpts({source: 'Service1'}) service: MyConfigurableService) {
    console.log(service.source) // log: Service1
  }
}

@Injectable()
class MyService2 {
  constructor(@UseOpts({source: 'Service2'}) service: MyConfigurableService) {
    console.log(service.source) // log: Service2
  }
}

WARNING

Using Opts decorator on a constructor parameter change the Scope of the provider to ProviderScope.INSTANCE.

Released under the MIT License.