Class Container<P>

The dependency injection container for checked-inject.

Type Parameters

Hierarchy

  • Container

Methods

Child Methods

  • Creates a child container. The child inherits all provided types and scopes from this container, but no changes to the child will affect this container.

    Returns Container<ChildGraph<P, never>>

    A child of this container

  • Type Parameters

    • Args extends any[]

    • P2 extends ProvideGraph = never

    Parameters

    • Optional f: ((child, ...args) => Container<P2>)
        • (child, ...args): Container<P2>
        • Parameters

          • child: Container<ChildGraph<FlatGraph<never>, never>>
          • Rest ...args: Args

          Returns Container<P2>

    Returns Subcomponent<Args, Merge<P, P2>>

    A Subcomponent that passes arguments to the given function to initialize a child container.

    Example

     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)

Provide Methods

  • Adds the given Scopes to this container. Any dependencies with this scope will be instantiated when first requested and memoized within this container.

    Type Parameters

    Parameters

    • Rest ...scope: S[]

      The scope or scopes to add to this container

    Returns Container<Provide<P, S extends any
        ? DepPair<S, never>
        : never>>

    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.

    Type Parameters

    Parameters

    • Rest ...modules: M

      The modules to apply to the container.

    Returns Container<Merge<P, Provides<M>>>

    This container, now typed to include all types provided within the given modules

    Example

    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])

    Example

    Apply function:

    const myContainer = Container.create().apply(ct => ct
    .provide(FooService, () => new FooService())
    .provide(BarService, () => new BarService())
    )

    const [fooService, barService] = myContainer.request([FooService, BarService])

    Example

    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))

    Type Parameters

    • K extends ResourceKey

    • Src extends any

    Parameters

    • key: K

      The key or class of the type to be provided

    • src: Src

      The DependencyKey to use to when key is requested

    Returns Container<Provide<P, DepPair<_ToBaseResource<K>, _DepsOf<Src>> | DepPair<IsSync<_ToBaseResource<K>>, IsSync<_ToBaseResource<K>>>>>

    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.

    Type Parameters

    • K extends ResourceKey

      The type of the key or class object

    • SrcK extends DependencyKey = never

      Identifies the source data for the provider

    • D = _DepsOf<SrcK>

      Identifies the dependencies of the provided type

    • Sync = RequireSync<D>

      Identifies the dependencies for the type to be resolved synchronously

    • S extends Scope = never

    Parameters

    • key: K

      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 value
      • deps, init:
        • deps: An object specifying which dependencies are required to provide the value
        • init: A function that accepts the dependencies specified by deps and returns the provided value

    Returns Container<Provide<P, PairForProvide<K, D, S> | PairForProvideIsSync<K, Sync, S>>>

    This container, now typed to include the provided type and its dependencies

    Example

    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().

    Type Parameters

    • T

    • K extends ResourceKey<Awaited<T>>

      The type of the key or class object

    • SrcK extends DependencyKey = never

      Identifies the source data for the provider

    • D = _DepsOf<SrcK>

      Identifies the dependencies of the provided type

    • S extends Scope = never

    Parameters

    • key: K

      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 promise
      • deps, init:
        • deps: An object specifying which dependencies are required to provide the value
        • init: An async function that accepts the dependencies specified by deps and returns the provided value

    Returns Container<Provide<P, PairForProvide<K, D, S> | PairForProvideNotSync<K>>>

    This container, now typed to include the provided type and its dependencies

    Example

    myContainer.provideAsync(
    NameKey,
    { NameService }, async ({ NameService }) => await NameService.getName(),
    )
  • Provides an instance of the target type for the given key or class.

    Type Parameters

    • K extends ResourceKey

      The type of the key or class object

    Parameters

    • key: K

      The key or class of the type to be provided

    • instance: Target<K, never>

      The provided instance

    Returns Container<Provide<P, DepPair<IsSync<_ToBaseResource<K>>, never> | DepPair<_ToBaseResource<K>, never>>>

    This container, now typed to include the provided type and its dependencies

Request Methods

  • Given a FactoryKey or similar, return the result of the factory function after applying args.

    Type Parameters

    • K extends Of<((...args) => any), any, any>

      Type of the factory key

    • Th extends RequestFailed<unknown>

      A this-type bound enforcing that K is safe to request

    • Args extends unknown[]

      The factory's parameter list type

    • Out = Target<K, P> extends ((...args) => O)
          ? O
          : unknown

      The factory's return type

    Parameters

    • this: Container<P> & Th
    • key: K

      A FactoryKey or similar key that resolves to a function

    • Rest ...args: Args

    Returns Out

  • Given 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.

    Type Parameters

    • K extends Of<((...args) => any), any, any>

      Type of the factory key

    • Th extends RequestFailed<unknown>

      A this-type bound enforcing that K is safe to request

    • Args extends unknown[]

      The factory's parameter list type

    • Out = Target<K, P> extends ((...args) => O)
          ? O
          : unknown

      The factory's return type

    Parameters

    • this: Container<P> & Th
    • key: K

      A FactoryKey or similar key that resolves to a possibly async function

    • Rest ...args: Args

    Returns Promise<Awaited<Out>>

  • Calls the given function with the requested dependencies and returns its output.

    Type Parameters

    • K extends DependencyKey

    • R

    • Th extends RequestFailed<unknown>

      A this-type bound enforcing that K is safe to request

    Parameters

    • this: Container<P> & Th
    • deps: K

      A key object specifying which dependencies to request

    • f: ((deps) => R)

      A function that accepts the dependencies specified by deps

        • (deps): R
        • Parameters

          Returns R

    Returns R

  • 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.

    Type Parameters

    • K extends DependencyKey

    • R

    • Th extends RequestFailed<unknown>

      A this-type bound enforcing that K is safe to request

    Parameters

    • this: Container<P> & Th
    • deps: K

      A key object specifying which dependencies to request

    • f: ((deps) => R | Promise<R>)

      A possibly async function that accepts the dependencies specified by deps

        • (deps): R | Promise<R>
        • Parameters

          Returns R | Promise<R>

    Returns Promise<R>

    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.

    Type Parameters

    Parameters

    Returns Target<K, P>

    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.

    Type Parameters

    Parameters

    Returns Promise<Target<K, P>>

    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.

    Type Parameters

    Parameters

    • deps: K

    Returns Promise<Target<K, P>>

    A promise resolving to the requested dependency

    Throws

    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.

    Type Parameters

    Parameters

    • key: K

      A key specifying the dependency to request

    Returns Target<K, P>

    The requested dependency

    Throws

    InjectError when not all transitive dependencies are met

Static Methods

Generated using TypeDoc