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.
Note: In most cases, prefer the inline form on Container or PartialContainer:
provides('token', ['dep'] as const, (dep) => value).
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.
// Prefer the inline form:
const container = Container
.providesValue("DependencyA", new A())
.providesValue("DependencyB", new B())
.provides("MyService", ["DependencyA", "DependencyB"] as const, (a: A, b: B) => new MyService(a, b));
// Use Injectable() when you need a reusable factory object:
const myServiceFactory = Injectable(
"MyService", ["DependencyA", "DependencyB"] as const, (a: A, b: B) => new MyService(a, b)
);
Creates a reusable Injectable factory function designed for services without dependencies.
Note: In most cases, prefer using
provides('token', () => value)directly on Container or PartialContainer instead.Injectable()is primarily needed when you want a reusable factory object — for example, to pass to Container.run().