Scopes
A scope says how long an instance lives and who shares it. Every provider has one; the default is singleton.
Singleton
One instance serves the whole container. DatabaseService and UserService from the quick start are singletons, which fits anything stateless or deliberately shared: clients, pools, repositories, configuration.
A singleton is always built against the root container, even when it is first requested through a scope. It can therefore never see a value that belongs to one scope.
Concurrent get() calls for a singleton that is still initialising all receive the same instance; it is built once.
Transient
Each resolution builds a new instance. Two inject() calls in one class give two instances:
A transient injected into a singleton is built once, together with the singleton, and lives as long as it does. A method that needs a new one on every call injects the Container and resolves the transient there.
A transient is not stored, so it is owned by whatever created it:
Request
A request-scoped service exists once per ScopedContainer. The name comes from servers, where a scope lasts for one HTTP request, but a scope can wrap any unit of work: a job, a message, a CLI command.
Creating a scope
container.createScope() returns a ScopedContainer. It resolves like a container and must be disposed when the work is done.
With TypeScript 5.2 or newer, await using disposes the scope at the end of the block:
Scopes are independent of each other. Any number can be open at once, each with its own RequestState, all sharing the root's singletons. Creating one is cheap; request-scoped services are built only when something asks for them.
A request-scoped service is found through the scope object it is resolved from. There is no ambient context and no AsyncLocalStorage, so pass the scope to the code that resolves from it.
After dispose(), get() rejects and getSync() and addInstance() throw a DIError with code ContainerDisposed. A second dispose() returns the first one. A scope that is never disposed stays linked to the singletons its services injected, which costs each of them somewhat more than a kilobyte per scope for as long as they live.
The current scope
inject(ScopedContainer) gives a request-scoped service, or a transient inside one, the scope it belongs to. Use it to resolve request-scoped services later, when inject(Container) would give the root:
Values supplied per scope
Data that exists only for one unit of work enters the scope through addInstance(). Declare a token with a scope for it:
createScope() also accepts a plain metadata object, read back with scope.getMetadata(key) and changed with scope.setMetadata(key, value). Metadata is not injectable; it is for the code that owns the scope.
Scope rules
A service may only depend on things that live at least as long as it does.
The container enforces the table. It never changes a service's scope to make a graph work.
ScopeMismatchError
Breaking the rule fails with a DIError with code ScopeMismatchError. The message names the path that led there:
The error is the same whether AuditService is requested from the root or from a scope, and whether it uses inject() or optional(). A transient in between does not hide it. Resolving a request-scoped service directly from the root container is the same error without a path.
There are three ways out: