Skip to content

kernel/di/container

Defined in: app/src/kernel/di/container.ts:64

A small, typed dependency injection container with constructor injection, lifetimes and child containers.

Instances that the container creates and that implement Disposable are owned by the container and disposed with it, in reverse order of creation. Values registered with useValue stay owned by the caller.

new Container(parent?): Container

Defined in: app/src/kernel/di/container.ts:77

Creates a root container. Use Container.createChild for child containers.

Container

Parent container; only used by createChild.

Container

get isDisposed(): boolean

Defined in: app/src/kernel/di/container.ts:208

Whether Container.dispose has been called.

boolean

createChild(): Container

Defined in: app/src/kernel/di/container.ts:200

Creates a child container. It sees all registrations of this container and can add or shadow its own. Disposing this container disposes its children first.

Container

dispose(): void

Defined in: app/src/kernel/di/container.ts:216

Disposes child containers, then every owned instance in reverse order of creation.

void

AggregateError when one or more instances failed to dispose.

Disposable.dispose

has(token): boolean

Defined in: app/src/kernel/di/container.ts:141

Tells whether this container or one of its parents provides the token.

Token<unknown>

The token to look up.

boolean

instantiate<T, D>(useClass): T

Defined in: app/src/kernel/di/container.ts:188

Constructs a class with dependencies from this container without registering it. The instance is owned by this container.

T

D extends readonly Token<unknown>[]

InjectableClass<T, D>

The class to construct.

T

register<T>(token, provider): this

Defined in: app/src/kernel/di/container.ts:89

Registers a provider for a token in this container. A child may register a token that a parent already provides; the child’s registration then wins inside the child.

T

Token<T>

The token to provide.

Provider<T>

How to obtain the value.

this

This container, for chaining.

ContainerError if the token is already registered in this container.

registerClass<T, D>(token, useClass, lifetime?): this

Defined in: app/src/kernel/di/container.ts:129

Registers a class. The type checker verifies that the class’s inject list matches its constructor parameters.

T

D extends readonly Token<unknown>[]

Token<T>

The token to provide.

InjectableClass<T, D>

The class to construct.

Lifetime = 'singleton'

Defaults to singleton.

this

registerFactory<T>(token, factory, lifetime?): this

Defined in: app/src/kernel/di/container.ts:114

Registers a factory function.

T

Token<T>

The token to provide.

(container) => T

Creates the value; receives the resolving container.

Lifetime = 'singleton'

Defaults to singleton.

this

registerValue<T>(token, value): this

Defined in: app/src/kernel/di/container.ts:104

Registers a fixed value.

T

Token<T>

The token to provide.

T

The value; the container does not dispose it.

this

resolve<T>(token): T

Defined in: app/src/kernel/di/container.ts:150

Returns the value for a token, creating it if needed.

T

Token<T>

The token to resolve.

T

ContainerError if nothing provides the token, or on a dependency cycle.

tryResolve<T>(token): T | undefined

Defined in: app/src/kernel/di/container.ts:179

Like Container.resolve, but returns undefined when nothing provides the token. Cycles and errors thrown while creating the value still propagate.

T

Token<T>

The token to resolve.

T | undefined


Defined in: app/src/kernel/di/container.ts:46

Thrown for missing registrations, duplicate registrations and dependency cycles.

  • Error

new ContainerError(message?): ContainerError

Defined in: node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1080

string

ContainerError

Error.constructor

new ContainerError(message?, options?): ContainerError

Defined in: node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1080

string

ErrorOptions

ContainerError

Error.constructor

readonly name: "ContainerError" = 'ContainerError'

Defined in: app/src/kernel/di/container.ts:47

Error.name

Defined in: app/src/kernel/di/container.ts:26

A class that the container can construct. Its static inject list names the tokens passed to the constructor, in order. There is no reflection and no decorator.

class TabService {
static readonly inject = [LoggerToken, ClockToken] as const;
constructor(private readonly logger: Logger, private readonly clock: Clock) {}
}

T

D extends readonly Token<unknown>[] = readonly Token<unknown>[]

new InjectableClass(…args): T

Defined in: app/src/kernel/di/container.ts:32

…TokenTypes<D>

T

readonly inject: D

Defined in: app/src/kernel/di/container.ts:31

Tokens resolved and passed to the constructor, in order.

Lifetime = "singleton" | "scoped" | "transient"

Defined in: app/src/kernel/di/container.ts:12

How long an instance created by the container lives.

  • singleton: one instance in the container that holds the registration, shared by all child containers.
  • scoped: one instance per container that resolves it (for example one per window or per module).
  • transient: a new instance on every resolution.

Provider<T> = { useValue: T; } | { lifetime?: Lifetime; useFactory: (container) => T; } | { lifetime?: Lifetime; useClass: InjectableClass<T>; }

Defined in: app/src/kernel/di/container.ts:36

Describes how the container obtains the value for a token.

T

{ useValue: T; }


{ lifetime?: Lifetime; useFactory: (container) => T; }

readonly optional lifetime?: Lifetime

readonly useFactory: (container) => T

Creates the value; it may resolve other tokens from the container.

Container

T


{ lifetime?: Lifetime; useClass: InjectableClass<T>; }