A unique Token identifying the Service within the container. This token is used to retrieve the instance from the container.
A zero-argument function that initializes and returns the Service instance. This can be any class instance, primitive, or complex value meant to be managed within the DI container.
Creates an Injectable factory function that requires dependencies.
The dependencies are specified as tokens, and the factory function will receive these dependencies as arguments in the order they are listed.
Important: This function requires TypeScript 5 or later due to the use of const
type parameters.
Users on TypeScript 4 and earlier must use InjectableCompat instead.
A unique Token identifying the Service within the container.
A readonly array of Tokens representing the dependencies required by the factory function. These will be resolved by the container and provided as arguments to the factory function.
A factory function whose parameters match the dependencies. This function should initialize and return an instance of the Service. The types and number of its parameters must exactly match the dependencies.
const dependencyB = 'DependencyB';
const container = Container
.providesValue("DependencyA", new A())
.providesValue("DependencyB", new B())
.provides(Injectable(
"MyService",
["DependencyA", dependencyB] as const, // "as const" can be omitted in TypeScript 5 and later
(a: A, b: B) => new MyService(a, b),
)
)
const myService = container.get("MyService");
Creates an Injectable factory function designed for services without dependencies. This is useful for simple services or values that don't depend on other parts of the system.