Resolves a dependency by its identifier without initialization or instantiation.
The identifier used to resolve the dependency from the container.
The resolved dependency.
A DependencyResolutionError if the dependency is not found.
const logger = inject<Logger>('Logger');
Resolves and instantiates a dependency using its constructor.
The identifier used to resolve the dependency from the container.
Set to true
to instantiate the dependency after resolution.
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.
The identifier used to resolve the dependency from the container.
Set to true
to instantiate the dependency.
If false
, resolution or instantiation errors return undefined
instead of throwing.
The resolved and instantiated dependency, or undefined
if resolution or instantiation fails.
Resolves a dependency without instantiating it, optionally failing silently.
The identifier used to resolve the dependency from the container.
Set to false
to skip instantiation.
If false
, resolution errors return undefined
instead of throwing.
The resolved dependency, or undefined
if not found.
Resolves a dependency and applies a custom initializer function to transform the result.
The identifier used to resolve the dependency.
A function to transform or initialize the dependency.
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.
The identifier used to resolve the dependency.
A function to transform or initialize the dependency.
If false
, resolution or initializer errors return undefined
instead of throwing.
The transformed dependency, or undefined
if resolution or initialization fails.
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.
Template: T
The original dependency type.
Template: U
The final return type after optional initialization or transformation.
Param: identifier
The identifier used to resolve the dependency.
See
Param: init
Optional: either
true
to instantiate via constructor,false
to skip, or a function to transform the dependency.Param: isNecessary
If
true
, throws on failure; iffalse
, returnsundefined
on resolution or initialization errors.Returns
The resolved dependency or result of initialization, or
undefined
if not necessary and resolution fails.Throws
A DependencyResolutionError if the dependency is not found (and necessary).
Throws
A NoInstantiationMethodError if instantiation is requested but no constructor exists.
Throws
An InitializationError if the initializer throws an error.
Throws
A InjectorError for unknown errors during resolution or transformation.
Example
Example
Example