• A function to inject a dependency from a DI (Dependency Injection) container into a variable.

    Type Parameters

    • T

      The type of the dependency to be injected.

    • U

      The type of the property to be injected.

    Parameters

    • identifier: Identifier

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

    • Optionalinit: true | InitDelegate<T, U>

      Optional an initializer function to transform the dependency before injection or true to instantiate the dependency if it has a constructor.

    • isNecessary: boolean = true

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

    Returns T | U | undefined

    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.

    Only throws errors if the dependency is necessary.

    A DependencyResolutionError if the dependency is not found.

    A InjectorError if an error occurs during the injection process.

    A NoInstantiationMethodError if the dependency does not have a constructor.

    An InitializationError if an error occurs during the initialization process.

    let myDependency = inject<MyDependency>('MyDependencyIdentifier');
    
    let logger = inject<ILogger>('ILogger_', (x: ILogger_) => x.getLogger('Tags'), false);