Skip to content

API Summary

This page summarizes the public core API. See the package README and TypeScript declarations for exact generic definitions.

Container

ts
import {
  Container,
  type ContainerOptions,
  type DependenciesMap,
  type Lazy,
  type LazySpec,
  type Module,
  type RegistrationKind,
  type Spec,
  type SpecMap,
} from '@inferdi/inferdi'
ts
class Container<T extends DependenciesMap = Record<never, never>> {
  constructor(options?: ContainerOptions)

  registerClass(key, Ctor, deps, kind?, lazyKey?)
  registerFactory(key, factory, kind?, lazyKey?)
  registerValue(key, value)
  override(key, value)
  use(fn)

  createScope()
  get(key)
  has(key)

  get disposed(): boolean
  dispose(): Promise<void>
  [Symbol.dispose](): void
  [Symbol.asyncDispose](): Promise<void>
}

Registration Methods

MethodUse
registerClassRegister a constructor and dependency tuple.
registerFactoryRegister custom construction logic.
registerValueRegister an externally owned singleton value.
overrideReplace an existing registration; rejects a key already cached locally.
useApply a module builder.

registerClass and registerFactory accept singleton, scoped, and transient lifetimes, plus an optional lazyKey companion. registerValue is always singleton and externally owned.

Treat the dependency tuple passed to registerClass as immutable after registration. The optimized constructor paths do not add a defensive copy.

The override timing guard checks only the current container's cache. It catches locally cached singleton/scoped values, registerValue, and repeated overrides, but it does not record transient resolutions or ancestor-owned values resolved through a child. Apply overrides before resolving the dependency graph.

Namespace Types

ts
namespace Container {
  type Resolve<C>
  type ResolveUnwrapped<C>
  type UnwrappedValue<C, K>
  type Providers<C>
}
TypeUse
Container.Resolve<C>Extract a flat { key: Value } map from a built container.
Container.ResolveUnwrapped<C>Like Resolve, but unwraps managed LazySpec companion entries to T; ordinary services with a .get() method stay unchanged.
Container.UnwrappedValue<C, K>Look up one unwrapped service type.
Container.Providers<C>Create a map of provider thunks for tests.

Public Types

ts
type Lazy<T> = { readonly get: () => T }
type RegistrationKind = 'singleton' | 'transient' | 'scoped'

interface ContainerOptions {
  readonly strict?: boolean
}

interface Spec<V, K extends RegistrationKind = 'singleton'> {
  readonly type: V
  readonly kind: K
}

type SpecMap<M, K extends RegistrationKind = 'singleton'> = {
  [P in keyof M]: Spec<M[P], K>
}

type Module<TIn extends DependenciesMap, TOut extends DependenciesMap> =
  (c: Container<TIn>) => Container<TIn & TOut>

Adapter API Shapes

Every adapter exports:

  • the integration function, such as inferdiFastify
  • skipInferdiDispose
  • MaybePromise
  • structural InferdiScope, InferdiRoot, and InferdiScopeOf helpers
  • framework-specific option and context helper types

Use the adapter pages for framework-specific generic names and lifecycle details.