ServiceCollection
Defined in: src/service-collection.ts:43
Collects service registrations, then builds a ServiceProvider.
Modeled on .NET’s IServiceCollection.
Example
Section titled “Example”const services = new ServiceCollection();services.addSingleton(Logger, ConsoleLogger);services.addScoped(UserService, [Logger]);const provider = services.buildServiceProvider({ validateScopes: true });Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ServiceCollection():
ServiceCollection
Returns
Section titled “Returns”ServiceCollection
Methods
Section titled “Methods”addKeyedScoped()
Section titled “addKeyedScoped()”Call Signature
Section titled “Call Signature”addKeyedScoped<
T>(token,factory,key):this
Defined in: src/service-collection.ts:125
Registers a keyed scoped service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedScoped<
T,A>(token,implementation,key,dependencies):this
Defined in: src/service-collection.ts:126
Registers a keyed scoped service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedScoped<
T>(token,implementation,key):this
Defined in: src/service-collection.ts:127
Registers a keyed scoped service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
addKeyedSingleton()
Section titled “addKeyedSingleton()”Call Signature
Section titled “Call Signature”addKeyedSingleton<
T>(token,factory,key):this
Defined in: src/service-collection.ts:117
Registers a keyed singleton, resolved with getKeyedService(token, key).
Keyed registrations are separate from plain ones: getService(token) never returns them.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedSingleton<
T,A>(token,implementation,key,dependencies):this
Defined in: src/service-collection.ts:118
Registers a keyed singleton, resolved with getKeyedService(token, key).
Keyed registrations are separate from plain ones: getService(token) never returns them.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedSingleton<
T>(token,implementation,key):this
Defined in: src/service-collection.ts:119
Registers a keyed singleton, resolved with getKeyedService(token, key).
Keyed registrations are separate from plain ones: getService(token) never returns them.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
addKeyedTransient()
Section titled “addKeyedTransient()”Call Signature
Section titled “Call Signature”addKeyedTransient<
T>(token,factory,key):this
Defined in: src/service-collection.ts:133
Registers a keyed transient service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedTransient<
T,A>(token,implementation,key,dependencies):this
Defined in: src/service-collection.ts:134
Registers a keyed transient service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addKeyedTransient<
T>(token,implementation,key):this
Defined in: src/service-collection.ts:135
Registers a keyed transient service. See addKeyedSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
addScoped()
Section titled “addScoped()”Call Signature
Section titled “Call Signature”addScoped<
T>(token,factory):this
Defined in: src/service-collection.ts:64
Registers a scoped service: one instance per scope. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addScoped<
T>(token):this
Defined in: src/service-collection.ts:65
Registers a scoped service: one instance per scope. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addScoped<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:66
Registers a scoped service: one instance per scope. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addScoped<
T>(token,implementation):this
Defined in: src/service-collection.ts:67
Registers a scoped service: one instance per scope. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addScoped<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:68
Registers a scoped service: one instance per scope. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
addSingleton()
Section titled “addSingleton()”Call Signature
Section titled “Call Signature”addSingleton<
T>(token,factory):this
Defined in: src/service-collection.ts:54
Registers a singleton: one instance for the root provider and all its scopes.
addSingleton(Class)/addSingleton(Class, [deps]): the class is its own token.addSingleton(token, Implementation, [deps]):depsare checked against the constructor’s parameters.addSingleton(token, (provider) => instance): a factory, may be async.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addSingleton<
T>(token):this
Defined in: src/service-collection.ts:55
Registers a singleton: one instance for the root provider and all its scopes.
addSingleton(Class)/addSingleton(Class, [deps]): the class is its own token.addSingleton(token, Implementation, [deps]):depsare checked against the constructor’s parameters.addSingleton(token, (provider) => instance): a factory, may be async.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addSingleton<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:56
Registers a singleton: one instance for the root provider and all its scopes.
addSingleton(Class)/addSingleton(Class, [deps]): the class is its own token.addSingleton(token, Implementation, [deps]):depsare checked against the constructor’s parameters.addSingleton(token, (provider) => instance): a factory, may be async.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addSingleton<
T>(token,implementation):this
Defined in: src/service-collection.ts:57
Registers a singleton: one instance for the root provider and all its scopes.
addSingleton(Class)/addSingleton(Class, [deps]): the class is its own token.addSingleton(token, Implementation, [deps]):depsare checked against the constructor’s parameters.addSingleton(token, (provider) => instance): a factory, may be async.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addSingleton<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:58
Registers a singleton: one instance for the root provider and all its scopes.
addSingleton(Class)/addSingleton(Class, [deps]): the class is its own token.addSingleton(token, Implementation, [deps]):depsare checked against the constructor’s parameters.addSingleton(token, (provider) => instance): a factory, may be async.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
addTransient()
Section titled “addTransient()”Call Signature
Section titled “Call Signature”addTransient<
T>(token,factory):this
Defined in: src/service-collection.ts:74
Registers a transient service: a new instance on every resolution. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addTransient<
T>(token):this
Defined in: src/service-collection.ts:75
Registers a transient service: a new instance on every resolution. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addTransient<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:76
Registers a transient service: a new instance on every resolution. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addTransient<
T>(token,implementation):this
Defined in: src/service-collection.ts:77
Registers a transient service: a new instance on every resolution. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”addTransient<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:78
Registers a transient service: a new instance on every resolution. Same forms as addSingleton.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
addValue()
Section titled “addValue()”addValue<
T>(token,value):this
Defined in: src/service-collection.ts:141
Registers an existing value (config, constants, an instance you built). Any value works, null and undefined included. Never disposed by the container.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
NoInfer<T>
Returns
Section titled “Returns”this
buildServiceProvider()
Section titled “buildServiceProvider()”buildServiceProvider(
options?):ServiceProvider
Defined in: src/service-collection.ts:171
Builds the provider. Registrations are copied, so changing this collection afterwards does not affect providers already built.
Parameters
Section titled “Parameters”options?
Section titled “options?”BuildOptions = {}
Returns
Section titled “Returns”getCircularDependencies()
Section titled “getCircularDependencies()”getCircularDependencies():
CircularDependency[]
Defined in: src/service-collection.ts:194
Every dependency cycle among plain registrations.
Returns
Section titled “Returns”getDependencyTree()
Section titled “getDependencyTree()”getDependencyTree(
token):DependencyTreeNode
Defined in: src/service-collection.ts:179
Dependency tree of token, following the last registration of each dependency.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”has<
T>(token,key?):boolean
Defined in: src/service-collection.ts:163
True if token (or token with key) is registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
Returns
Section titled “Returns”boolean
remove()
Section titled “remove()”remove<
T>(token):this
Defined in: src/service-collection.ts:146
Removes every registration of token, keyed ones included.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
Returns
Section titled “Returns”this
replace()
Section titled “replace()”Call Signature
Section titled “Call Signature”replace<
T>(token,factory):this
Defined in: src/service-collection.ts:153
Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”replace<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:154
Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”replace<
T>(token,implementation):this
Defined in: src/service-collection.ts:155
Replaces the plain registrations of token, keeping the lifetime of the last one (singleton if there was none).
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
tryAddScoped()
Section titled “tryAddScoped()”Call Signature
Section titled “Call Signature”tryAddScoped<
T>(token,factory):this
Defined in: src/service-collection.ts:94
Like addScoped, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddScoped<
T>(token):this
Defined in: src/service-collection.ts:95
Like addScoped, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddScoped<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:96
Like addScoped, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddScoped<
T>(token,implementation):this
Defined in: src/service-collection.ts:97
Like addScoped, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddScoped<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:98
Like addScoped, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
tryAddSingleton()
Section titled “tryAddSingleton()”Call Signature
Section titled “Call Signature”tryAddSingleton<
T>(token,factory):this
Defined in: src/service-collection.ts:84
Like addSingleton, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddSingleton<
T>(token):this
Defined in: src/service-collection.ts:85
Like addSingleton, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddSingleton<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:86
Like addSingleton, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddSingleton<
T>(token,implementation):this
Defined in: src/service-collection.ts:87
Like addSingleton, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddSingleton<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:88
Like addSingleton, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
tryAddTransient()
Section titled “tryAddTransient()”Call Signature
Section titled “Call Signature”tryAddTransient<
T>(token,factory):this
Defined in: src/service-collection.ts:104
Like addTransient, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
factory
Section titled “factory”ServiceFactory<NoInfer<T>>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddTransient<
T>(token):this
Defined in: src/service-collection.ts:105
Like addTransient, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Newable<T, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddTransient<
T,A>(token,dependencies):this
Defined in: src/service-collection.ts:106
Like addTransient, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Newable<T, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddTransient<
T>(token,implementation):this
Defined in: src/service-collection.ts:107
Like addTransient, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, []>
Returns
Section titled “Returns”this
Call Signature
Section titled “Call Signature”tryAddTransient<
T,A>(token,implementation,dependencies):this
Defined in: src/service-collection.ts:108
Like addTransient, but does nothing if token is already registered.
Type Parameters
Section titled “Type Parameters”T
A extends unknown[] = any[]
Parameters
Section titled “Parameters”Token<T>
implementation
Section titled “implementation”Newable<NoInfer<T>, A>
dependencies
Section titled “dependencies”Dependencies<A>
Returns
Section titled “Returns”this
visualizeCircularDependencies()
Section titled “visualizeCircularDependencies()”visualizeCircularDependencies():
string
Defined in: src/service-collection.ts:229
Every dependency cycle as text.
Returns
Section titled “Returns”string
visualizeDependencyTree()
Section titled “visualizeDependencyTree()”visualizeDependencyTree(
token):string
Defined in: src/service-collection.ts:216
The dependency tree of token as text.
Parameters
Section titled “Parameters”Returns
Section titled “Returns”string