Docs/API Reference/Kernel

Kernel API: dependency injection

A provider definition includes a token and one of useValue, useFactory or useClass. It can also define inject tokens and singleton.

2 min read

Public types and values#

  • Container
  • ProviderDef<T>
  • Token<T>
  • container
  • app()
  • app(token)
  • Injectable()
  • Inject(token)
  • ServiceProvider

Provider definitions#

A provider definition includes a token and one of useValue, useFactory or useClass. It can also define inject tokens and singleton.

ts
container.register({
  token: "feature:clock",
  useFactory: () => ({ now: () => new Date() }),
  singleton: true,
});

Factories receive resolved dependencies as positional arguments in the order listed in inject. Registered definitions are singleton by default; set singleton: false for a transient registered provider.

Resolution behavior#

resolve(token) returns a promise. Registered values/factories/classes are resolved and cached unless singleton is disabled. If no binding exists and the token is a class, the container attempts constructor injection and caches the instance. An unregistered string or symbol token fails with an internal application error.

has(token) checks for a provider or resolved instance. clear() clears cached instances/in-flight resolutions while retaining registrations; reset() clears registrations and instances.

Decorator requirements#

@Injectable() is a marker decorator; constructor injection relies on reflected design:paramtypes. @Inject(token) overrides the token for a specific constructor parameter. Ensure your TypeScript setup emits metadata and the reflect-metadata runtime is loaded. A dependency imported only with import type cannot be used as a runtime class token.

veap
Preparing0%