Function ClassInjectable

  • Creates an Injectable factory function for an InjectableClass.

    Type Parameters

    • Class extends {
          dependencies: any;
          new (...args: any[]): any;
      }
    • Dependencies extends any[]
    • Token extends TokenType
    • Tokens extends any

    Parameters

    • token: Token

      Token identifying the Service.

    • cls: Class

      InjectableClass to instantiate.

    Returns InjectableFunction<ServicesFromTokenizedParams<Tokens, Dependencies>, Tokens, Token, ConstructorReturnType<Class>>

    class Logger {
    static dependencies = ["config"] as const;
    constructor(private config: string) {}
    public print() {
    console.log(this.config);
    }
    }

    const container = Container
    .providesValue("config", "value")
    .provides(ClassInjectable("logger", Logger));

    container.get("logger").print(); // prints "value"

    It is recommended to use the Container.provideClass() method. The example above is equivalent to:

    const container = Container
    .providesValue("config", "value")
    .providesClass("logger", Logger);
    container.get("logger").print(); // prints "value"
  • Creates a reusable class Injectable whose class lookup is deferred until the Service is first resolved. The provider and the constructor it returns are memoized independently from the Service, so dependency metadata and construction always use the same class.

    Type Parameters

    • Class extends {
          dependencies: any;
          new (...args: any[]): any;
      }
    • Dependencies extends any[]
    • Token extends TokenType
    • Tokens extends any

    Parameters

    • token: Token

      Token identifying the Service.

    • getClass: (() => Class)

      Zero-argument function returning the InjectableClass to instantiate.

    Returns InjectableFunction<ServicesFromTokenizedParams<Tokens, Dependencies>, Tokens, Token, ConstructorReturnType<Class>>

    const injectable = ClassInjectable("logger", () => Logger);
    const container = Container.providesValue("config", config).provides(injectable);