kernel/di/container
Classes
Section titled “Classes”Container
Section titled “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.
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Container(
parent?):Container
Defined in: app/src/kernel/di/container.ts:77
Creates a root container. Use Container.createChild for child containers.
Parameters
Section titled “Parameters”parent?
Section titled “parent?”Parent container; only used by createChild.
Returns
Section titled “Returns”Accessors
Section titled “Accessors”isDisposed
Section titled “isDisposed”Get Signature
Section titled “Get Signature”get isDisposed():
boolean
Defined in: app/src/kernel/di/container.ts:208
Whether Container.dispose has been called.
Returns
Section titled “Returns”boolean
Methods
Section titled “Methods”createChild()
Section titled “createChild()”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.
Returns
Section titled “Returns”dispose()
Section titled “dispose()”dispose():
void
Defined in: app/src/kernel/di/container.ts:216
Disposes child containers, then every owned instance in reverse order of creation.
Returns
Section titled “Returns”void
Throws
Section titled “Throws”AggregateError when one or more instances failed to dispose.
Implementation of
Section titled “Implementation of”has(
token):boolean
Defined in: app/src/kernel/di/container.ts:141
Tells whether this container or one of its parents provides the token.
Parameters
Section titled “Parameters”Token<unknown>
The token to look up.
Returns
Section titled “Returns”boolean
instantiate()
Section titled “instantiate()”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.
Type Parameters
Section titled “Type Parameters”T
D extends readonly Token<unknown>[]
Parameters
Section titled “Parameters”useClass
Section titled “useClass”InjectableClass<T, D>
The class to construct.
Returns
Section titled “Returns”T
register()
Section titled “register()”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.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
The token to provide.
provider
Section titled “provider”Provider<T>
How to obtain the value.
Returns
Section titled “Returns”this
This container, for chaining.
Throws
Section titled “Throws”ContainerError if the token is already registered in this container.
registerClass()
Section titled “registerClass()”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.
Type Parameters
Section titled “Type Parameters”T
D extends readonly Token<unknown>[]
Parameters
Section titled “Parameters”Token<T>
The token to provide.
useClass
Section titled “useClass”InjectableClass<T, D>
The class to construct.
lifetime?
Section titled “lifetime?”Lifetime = 'singleton'
Defaults to singleton.
Returns
Section titled “Returns”this
registerFactory()
Section titled “registerFactory()”registerFactory<
T>(token,factory,lifetime?):this
Defined in: app/src/kernel/di/container.ts:114
Registers a factory function.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
The token to provide.
factory
Section titled “factory”(container) => T
Creates the value; receives the resolving container.
lifetime?
Section titled “lifetime?”Lifetime = 'singleton'
Defaults to singleton.
Returns
Section titled “Returns”this
registerValue()
Section titled “registerValue()”registerValue<
T>(token,value):this
Defined in: app/src/kernel/di/container.ts:104
Registers a fixed value.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
The token to provide.
T
The value; the container does not dispose it.
Returns
Section titled “Returns”this
resolve()
Section titled “resolve()”resolve<
T>(token):T
Defined in: app/src/kernel/di/container.ts:150
Returns the value for a token, creating it if needed.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
The token to resolve.
Returns
Section titled “Returns”T
Throws
Section titled “Throws”ContainerError if nothing provides the token, or on a dependency cycle.
tryResolve()
Section titled “tryResolve()”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.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
The token to resolve.
Returns
Section titled “Returns”T | undefined
ContainerError
Section titled “ContainerError”Defined in: app/src/kernel/di/container.ts:46
Thrown for missing registrations, duplicate registrations and dependency cycles.
Extends
Section titled “Extends”Error
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ContainerError(
message?):ContainerError
Defined in: node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1080
Parameters
Section titled “Parameters”message?
Section titled “message?”string
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Error.constructor
Constructor
Section titled “Constructor”new ContainerError(
message?,options?):ContainerError
Defined in: node_modules/.pnpm/typescript@6.0.3/node_modules/typescript/lib/lib.es5.d.ts:1080
Parameters
Section titled “Parameters”message?
Section titled “message?”string
options?
Section titled “options?”ErrorOptions
Returns
Section titled “Returns”Inherited from
Section titled “Inherited from”Error.constructor
Properties
Section titled “Properties”
readonlyname:"ContainerError"='ContainerError'
Defined in: app/src/kernel/di/container.ts:47
Overrides
Section titled “Overrides”Error.name
Interfaces
Section titled “Interfaces”InjectableClass
Section titled “InjectableClass”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.
Example
Section titled “Example”class TabService { static readonly inject = [LoggerToken, ClockToken] as const; constructor(private readonly logger: Logger, private readonly clock: Clock) {}}Type Parameters
Section titled “Type Parameters”T
D extends readonly Token<unknown>[] = readonly Token<unknown>[]
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new InjectableClass(…
args):T
Defined in: app/src/kernel/di/container.ts:32
Parameters
Section titled “Parameters”…TokenTypes<D>
Returns
Section titled “Returns”T
Properties
Section titled “Properties”inject
Section titled “inject”
readonlyinject:D
Defined in: app/src/kernel/di/container.ts:31
Tokens resolved and passed to the constructor, in order.
Type Aliases
Section titled “Type Aliases”Lifetime
Section titled “Lifetime”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
Section titled “Provider”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.
Type Parameters
Section titled “Type Parameters”T
Union Members
Section titled “Union Members”Type Literal
Section titled “Type Literal”{ useValue: T; }
Type Literal
Section titled “Type Literal”{ lifetime?: Lifetime; useFactory: (container) => T; }
lifetime?
Section titled “lifetime?”
readonlyoptionallifetime?:Lifetime
useFactory
Section titled “useFactory”
readonlyuseFactory: (container) =>T
Creates the value; it may resolve other tokens from the container.
Parameters
Section titled “Parameters”container
Section titled “container”Returns
Section titled “Returns”T
Type Literal
Section titled “Type Literal”{ lifetime?: Lifetime; useClass: InjectableClass<T>; }