A function to inject a dependency from a DI (Dependency Injection) container into a variable. This is the actual implementation that handles all overload variants.

The original dependency type.

The final return type after optional initialization or transformation.

The identifier used to resolve the dependency.

Optional: either true to instantiate via constructor, false to skip, or a function to transform the dependency.

If true, throws on failure; if false, returns undefined on resolution or initialization errors.

The resolved dependency or result of initialization, or undefined if not necessary and resolution fails.

A DependencyResolutionError if the dependency is not found (and necessary).

A NoInstantiationMethodError if instantiation is requested but no constructor exists.

An InitializationError if the initializer throws an error.

A InjectorError for unknown errors during resolution or transformation.

const service = inject<Service>('Service');
const instance = inject<Service>('Service', true);
const logger = inject<ILogger>('ILogger_', (x) => x.getLogger('Module'), false);
  • Resolves a dependency by its identifier without initialization or instantiation.

    Type Parameters

    • T

      The expected type of the dependency.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency from the container.

    Returns T

    The resolved dependency.

    A DependencyResolutionError if the dependency is not found.

    const logger = inject<Logger>('Logger');
    
  • Resolves and instantiates a dependency using its constructor.

    Type Parameters

    • T

      The expected class type of the dependency.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency from the container.

    • shouldInit: true

      Set to true to instantiate the dependency after resolution.

    Returns T

    The resolved and instantiated dependency.

    A DependencyResolutionError if the dependency is not found.

    A NoInstantiationMethodError if the dependency has no constructor.

    An InitializationError if instantiation fails.

    const instance = inject<Service>('Service', true);
    
  • Resolves and instantiates a dependency using its constructor, optionally failing silently.

    Type Parameters

    • T

      The expected class type of the dependency.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency from the container.

    • shouldInit: true

      Set to true to instantiate the dependency.

    • isNecessary: false

      If false, resolution or instantiation errors return undefined instead of throwing.

    Returns T | undefined

    The resolved and instantiated dependency, or undefined if resolution or instantiation fails.

    const instance = inject<Service>('OptionalService', true, false);
    if (instance) instance.doSomething();
  • Resolves a dependency without instantiating it, optionally failing silently.

    Type Parameters

    • T

      The expected type of the dependency.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency from the container.

    • shouldInit: false

      Set to false to skip instantiation.

    • isNecessary: false

      If false, resolution errors return undefined instead of throwing.

    Returns T | undefined

    The resolved dependency, or undefined if not found.

    const config = inject<Config>('Config', false, false) ?? getDefaultConfig();
    
  • Resolves a dependency and applies a custom initializer function to transform the result.

    Type Parameters

    • T

      The original dependency type.

    • U

      The final return type after initialization.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency.

    • init: InitDelegate<T, U>

      A function to transform or initialize the dependency.

    Returns U

    The transformed dependency.

    A DependencyResolutionError if the dependency is not found.

    An InitializationError if the initializer throws.

    const client = inject<Api>('Api', (api) => api.getClient());
    
  • Resolves a dependency and applies a custom initializer function, optionally failing silently.

    Type Parameters

    • T

      The original dependency type.

    • U

      The final return type after initialization.

    Parameters

    • identifier: Identifier

      The identifier used to resolve the dependency.

    • init: InitDelegate<T, U>

      A function to transform or initialize the dependency.

    • isNecessary: false

      If false, resolution or initializer errors return undefined instead of throwing.

    Returns U | undefined

    The transformed dependency, or undefined if resolution or initialization fails.

    const db = inject<Database, Pool>('Database', (d) => d.getPool(), false);
    if (db) db.query('SELECT * FROM users');