Tokens
A token is the identity of a dependency. Every @Injectable() class has one automatically. You create one yourself when the dependency is not a class you can name: an interface with several implementations, a configuration object, a value supplied at runtime.
Creating a token
The type parameter is what inject(CLOCK) and container.get(CLOCK) return. The name appears in error messages and has no other role: a token is identified by the object, so two tokens created with the same name are different tokens. Export the token and import it wherever it is needed.
A token on its own resolves to nothing. It needs a provider, which can come from any of:
To get the token of a decorated class, call getInjectableToken(UserService). A token can also be named after a class, abstract ones included: InjectionToken.create(Repository) has the type and the name of Repository.
Tokens with a schema
A schema turns a token into a parameterised dependency. Whoever resolves it passes arguments, and the provider receives them validated.
The schema can come from any Standard Schema library; these docs use Zod. Three rules apply:
- Callers pass the schema's input type, the provider receives its output type. Above,
regionmay be omitted by the caller and is always present in the constructor. - Invalid arguments fail with
TokenValidationError. The message names what is wrong, anderror.context.issuesholds the path and the message of each issue. The arguments themselves are not part of the error. - Validation must be synchronous. A schema that validates asynchronously, such as one with an async refinement, is rejected with the same error.
Whether the second argument is required follows from the schema. If the schema accepts undefined (for example bucketOptions.optional()), the token can be resolved without arguments. If it does not, leaving them out changes the result type to an error message that names the missing keys, so the mistake shows up at the first use of the value.
One instance per argument value
Instances are stored under the validated arguments, compared by value. Key order does not matter and nested values are compared in full.
A property that is undefined counts as absent. Date, Map, Set, URL, RegExp and typed arrays of up to 1 kB are compared by value, whatever the schema creates anew for each call. Other values that are not plain objects or arrays, such as class instances, functions and larger buffers, are compared by identity. Arguments that contain themselves are refused.
Bound tokens
InjectionToken.bound() fixes the arguments, producing a token that is resolved without any. Use it to give a configured instance a name.
A bound token resolves to the same instance as the base token with equal arguments, so inject(UPLOADS) and inject(BUCKET, { name: 'uploads' }) share one S3Bucket.
Factory tokens
InjectionToken.factory() computes the arguments when the token is first resolved. The function receives a factory context, so it can read other services.
The function runs at most once per root container, and always against the root container, even when the token is first resolved through a scope. It is not part of any construction, so inject() throws in it and ctx.inject() of something request-scoped is a ScopeMismatchError. The value is stored on the container, not on the token: two containers compute it separately. Listeners added with ctx.addDestroyListener() run when the container is disposed.
An async function makes the first resolution asynchronous: use get(), or inject() it from a service that is itself resolved with get().
Refining a bound token's type
InjectionToken.refineType<T>() changes the type a bound token resolves to, without changing the token. It is for the case where the arguments select a more specific type than the base token declares.
This is a type assertion. The container does not check it.
Tokens that declare a scope
A token can carry a scope and no provider at all. It is then a slot that code fills per scope with addInstance():
Declaring the scope makes misuse visible: resolving REQUEST_ID from the root container, or injecting it into a singleton, reports a scope mismatch instead of a missing provider. In a scope where no value was added, resolving it fails with ProviderNotFound. Scopes shows it in use. The options object also accepts schema.