A Subcomponent that passes arguments to the given function to initialize a child container.
const myContainer = Container.create()
.provide(User, MyScope, Inject.construct(User, NameKey, IdKey))
const mySubcomponent = myContainer.createSubcomponent(
(ct, name: string, id: string) => ct
.addScope(MyScope)
.provideInstance(NameKey, name)
.provideInstance(IdKey, id)
)
const childContainer = mySubcomponent('Alice', '123')
const user = childContainer.request(User)
Adds the given Scopes to this container. Any dependencies with this scope will be instantiated when first requested and memoized within this container.
Rest
...scope: S[]The scope or scopes to add to this container
This container, now typed to include the added scopes
Applies the given Modules or Items to this container. All scopes and dependencies provided with the modules will be applied to this container.
Rest
...modules: MThe modules to apply to the container.
This container, now typed to include all types provided within the given modules
Apply Module object:
const MyModule = Module(ct => ct
.provide(FooService, () => new FooService())
.provide(BarService, () => new BarService())
)
const myContainer = Container.create().apply(MyModule)
const [fooService, barService] = myContainer.request([FooService, BarService])
Apply function:
const myContainer = Container.create().apply(ct => ct
.provide(FooService, () => new FooService())
.provide(BarService, () => new BarService())
)
const [fooService, barService] = myContainer.request([FooService, BarService])
Use apply
to merge modules:
const FooModule = Module(ct => ct
.provide(FooService, () => new FooService())
)
const BarModule = Module(ct => ct
.provide(FooService, () => new FooService())
)
const MyModule = Module(ct => ct
.provide(BazService,
Inject.construct(BazService, FooService, BarService))
.apply(FooModule, BarModule)
)
const myContainer = Container.create().apply(MyModule)
const [fooService, barService] = myContainer.request([FooService, BarService])
Binds the resolved value of src to key.
In other words, when key is requested, src will be used to provide the value.
Equivalent to provide(key, Inject.from(src))
The key or class of the type to be provided
The DependencyKey to use to when key is requested
This container, now typed to include the provided type and its dependencies
Specifies how to resolve the target type of the given key or class.
The type of the key or class object
Identifies the source data for the provider
Identifies the dependencies of the provided type
Identifies the dependencies for the type to be resolved synchronously
The key or class of the type to be provided
Rest
...args: [init: ComputedKey<Target<K, never>, any, D, Sync, P>] | [deps: SrcK, init: ((deps) => Target<K, never>)] | [init: (() => Target<K, never>)] | [scope: NonEmptyScopeList<S>, init: ComputedKey<Target<K, never>, any, D, Sync, P>] | [scope: NonEmptyScopeList<S>, deps: SrcK, init: ((deps) => Target<K, never>)] | [scope: NonEmptyScopeList<S>, init: (() => Target<K, never>)]The remaining arguments. These include:
scope
(optional): The Scope or ScopeList to which to bind the provided value
And one of the following:init
: A function returning the provided value, or a ComputedKey yielding the provided valuedeps, init
:deps
and returns the provided valueThis container, now typed to include the provided type and its dependencies
Provide the TypeKey NameKey
with a function returning a string:
myContainer.provide(NameKey, () => 'Alice')
Provide the TypeKey IdKey
within the Scope MyScope
,
so the function is only called once per container with MyScope
myContainer.provide(IdKey, MyScope, () => '123')
Provide an instance of User
using a dependency object and a function.
The dependency object can be an object with DependencyKey properties, like so:
myContainer.provide(User, {
name: NameKey,
id: IdKey,
}, ({ name, id }) => new User(name, user))
Provide User
using a ComputedKey, {@link Inject.construct}
myContainer.provide(User, Inject.construct(User, NameKey, IdKey))
Specifies how to asynchronously resolve the target type of the given key or class. The signature is the same as provide, except that a Promise to the target type can be provided. The provided type cannot be directly requested through request. It can, however be requested through requestAsync or by using key.Async().
The type of the key or class object
Identifies the source data for the provider
Identifies the dependencies of the provided type
The key or class of the type to be provided
Rest
...args: [init: ComputedKey<T, any, D, any, P>] | [deps: SrcK, init: ((deps) => T)] | [init: (() => T)] | [scope: NonEmptyScopeList<S>, init: ComputedKey<T, any, D, any, P>] | [scope: NonEmptyScopeList<S>, deps: SrcK, init: ((deps) => T)] | [scope: NonEmptyScopeList<S>, init: (() => T)]The remaining arguments. These include:
scope
(optional): The Scope or ScopeList to which to bind the provided value
And one of the following:init
: An async function returning the provided value,
or a ComputedKey yielding the provided value or a promisedeps, init
:deps
and returns the provided valueThis container, now typed to include the provided type and its dependencies
myContainer.provideAsync(
NameKey,
{ NameService }, async ({ NameService }) => await NameService.getName(),
)
Provides an instance of the target type for the given key or class.
The type of the key or class object
The key or class of the type to be provided
The provided instance
This container, now typed to include the provided type and its dependencies
Given a FactoryKey or similar, return the result of the factory function after applying args.
A FactoryKey or similar key that resolves to a function
Rest
...args: ArgsGiven a FactoryKey or similar, asynchronously return the result of the factory function after applying args.
It is safe to call method when some dependencies cannot be resolved synchronously.
A FactoryKey or similar key that resolves to a possibly async function
Rest
...args: ArgsCalls the given function with the requested dependencies and returns its output.
A this-type bound enforcing that K is safe to request
Asynchronously calls the given function with the requested dependencies and returns its output.
It is safe to call method when some dependencies cannot be resolved synchronously.
A this-type bound enforcing that K is safe to request
A promise resolving to the output of f
Requests the dependency specified by the given key, statically checking that the dependency is safe to request.
The requested dependency
Asynchronously requests the dependency specified by the given key, statically checking that the dependency is safe to request.
It is safe to call method when some dependencies cannot be resolved synchronously.
A promise resolving to the requested dependency
Asynchronously requests the dependency specified by the given key, without statically checking that the dependency is safe to request.
A promise resolving to the requested dependency
InjectError when not all transitive dependencies are met
Requests the dependency specified by the given key, without statically checking that the dependency is safe to request.
A key specifying the dependency to request
The requested dependency
InjectError when not all transitive dependencies are met
Static
createCreates a new Container instance.
Generated using TypeDoc
The dependency injection container for
checked-inject
.