• A decorator to inject a dependency from a DI (Dependency Injection) container into a class property.

    Type Parameters

    • T

      The type of the dependency to be injected.

    • U

      The type of the property to be injected.

    Parameters

    • identifier: string

      The identifier used to resolve the class in the DI container.

    • Optionalinit: InitDelegate<T, U>

      An optional initializer function to transform the dependency before injection.

    • necessary: boolean = true

      If true, throws an error if the dependency is not found.

    Returns ((target: unknown, propertyKey: string | symbol) => void)

    The resolved dependency or undefined if the dependency is not necessary and not found, or throws an error if the dependency is necessary and not found.

      • (target, propertyKey): void
      • Parameters

        • target: unknown
        • propertyKey: string | symbol

        Returns void

    A DependencyResolutionError if the dependency is not found and necessary.

    class MyClass {
    @Inject<MyDependency>('MyDependencyIdentifier')
    private myDependency!: MyDependency;
    }
    class MyClass {
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('Tags'), false)
    private _logger?: ILogger;
    }