dn-ioc

13 · Reference

Public API Reference

Everything dn-ioc exports comes from 'dn-ioc'—six runtime functions and fifteen TypeScript types, with zero sub-path imports or global side effects. Use this chapter whenever you want to double-check an exact parameter type, return value, or handle invariant.

functions.ts
1export const publicApi = {2  token: token satisfies <T>(description?: string) => Token<T>,3  provide: provide satisfies <T>(factory: Factory<T>, options?: ProviderOptions) => Ref<T>,4  provideFor: provideFor satisfies <T>(key: InjectKey<T>, factory: Factory<T>, options?: ProviderOptions) => ProviderDef<T>,5  bundleProviders: bundleProviders satisfies (...inputs: ProviderInput[]) => ProviderBundle,6  isProvideRef: isProvideRef satisfies (value: unknown) => value is Ref<unknown>,7  bootstrapApp: bootstrapApp satisfies (fn: BootstrapAppFn, options?: BootstrapAppOptions) => App,8}

Runtime Functions

token<T>(description?)

Signature: function token<T>(description?: string): Token<T>

Creates a pure dependency key carrying the TypeScript type T and zero runtime implementation.

  • description (optional string): Human-readable label shown in No provider for token: Name and Circular dependency detected: .... When omitted or empty, diagnostics print <anonymous>. Two separate token('Same') calls always produce distinct keys—identity never comes from the description string.
  • Async contracts: Put Promise inside T (token<Promise<Settings>>('Settings')) so both inject() callers and provideFor() implementations agree on the async return type.

provide<T>(factory, options?)

Signature: function provide<T>(factory: Factory<T>, options?: ProviderOptions): Ref<T>

Wraps factory in a self-providing Ref<T> handle.

  • factory: (ctx: Context) => T. If no binding for this Ref exists in the active scope or its ancestors when inject() is called, the Ref installs its default factory into the active scope, runs it once, and caches the result (or shares the in-flight Promise for an async factory). Sync throws and async rejections are not cached, allowing retries while the app is running.
  • Diagnostic name: Taken from factory.name (for example, const UserService = (ctx: Context) => { ... }; provide(UserService) reports UserService, whereas an unnamed inline arrow reports <anonymous>).
  • options?.providers: Optional private child subtree visible only to factory and its descendants. Snapshotted when provide() is called and validated lazily when that subtree is first resolved.

provideFor<T>(key, factory, options?)

Signature: function provideFor<T>(key: InjectKey<T>, factory: Factory<T>, options?: ProviderOptions): ProviderDef<T>

Binds an implementation factory to key—accepting either a Token<T> or a Ref<T>—and returns a ProviderDef<T> ready to install in { providers } or bundleProviders().

  • Validates key immediately when provideFor() is called; passing anything other than a real Token or Ref throws Invalid inject key received.
  • options?.providers: Optional private child subtree visible to this bound factory and its descendants; snapshotted on creation.

bundleProviders(...inputs)

Signature: function bundleProviders(...inputs: ProviderInput[]): ProviderBundle

Packs any mix of Refs, ProviderDef bindings, nested arrays, and other ProviderBundles into one reusable ProviderBundle.

  • Shallow-copies input arrays at creation time so later array mutations cannot alter the bundle.
  • Installing a bundle flattens its items left-to-right in source order directly into the receiving scope—it does not create an isolated child scope of its own.

isProvideRef(value)

Signature: function isProvideRef(value: unknown): value is Ref<unknown>

Type guard returning true only when value is a genuine Ref returned by provide(). Returns false for Token, ProviderDef, ProviderBundle, primitives, null, and cloned objects ({ ...someRef }).

bootstrapApp(fn, options?)

Signature: function bootstrapApp(fn: BootstrapAppFn, options?: BootstrapAppOptions): App

Creates an isolated root scope, validates and installs options?.providers synchronously, and returns an App handle without executing fn or any provider factory yet.

  • fn: (ctx: Context) => void | Promise<void>. Runs when app.start() is called. Must return void or Promise<void>.
  • options?.providers: Root-level bindings, refs, arrays, and bundles. Invalid entries throw Invalid provider input received synchronously before an App handle is returned.

Context & App Lifecycle Interfaces

Context (InjectFn & OnDisposeFn)

Every provider Factory<T> and BootstrapAppFn receives a Context object with two functions:

  • inject: InjectFn (<T>(key: InjectKey<T>) => T) — Resolves key starting from the provider's owning scope upward. Returns synchronous values directly and shared Promises for async factories. Remains callable inside returned methods after the factory finishes, up until app.stop() is called.
  • onDispose: OnDisposeFn ((fn: () => void | Promise<void>) => void) — Registers a cleanup hook run in LIFO (newest-first) order during app.stop(). Callable only while its own factory is actively running and before stop(). Returning anything other than void or Promise<void> is a TypeScript compile error—wrap non-void teardown expressions in braces (onDispose(() => { server.close() })).

App (start, stop & AsyncDisposable)

The handle returned by bootstrapApp() exposes three members:

  • start(): Promise<void> — Runs the root callback once and awaits any Promise it returns. Concurrent or repeated calls return the exact same Promise. If startup fails, start() automatically runs stop() to roll back already-acquired resources before rejecting. Once stop() has been called, start() rejects with Cannot start an app that has been stopped.
  • stop(): Promise<void> — Synchronously closes inject() across the entire app, then awaits registered onDispose hooks newest-first. Idempotent across concurrent or repeated calls. Even if a hook fails, all remaining hooks still run.
  • [Symbol.asyncDispose](): Promise<void> — Points to the exact same stop function so await using app = bootstrapApp(...) tears the app down automatically at block exit. When native Symbol.asyncDispose exists on the runtime, App defines [Symbol.asyncDispose]; on older runtimes without the native symbol (such as Node 18), it falls back to [Symbol.for('Symbol.asyncDispose')] to match TypeScript's downlevel await using lookup.
types.ts
1export type { ProviderBundle, ProviderDef, Ref, Token }23export type RefType<T> = T extends Ref<infer U> ? U : never4export type InjectKey<T> = Token<T> | Ref<T>5export type ProviderInput = Ref<unknown> | ProviderDef<unknown> | ProviderBundle | readonly ProviderInput[]67export type ProviderOptions = {8  providers?: readonly ProviderInput[]9}1011export type InjectFn = <T>(key: InjectKey<T>) => T12export type OnDisposeFn = (fn: () => void | Promise<void>) => void1314export interface Context {15  inject: InjectFn16  onDispose: OnDisposeFn17}1819export type Factory<T> = (ctx: Context) => T20export type BootstrapAppFn = (ctx: Context) => void | Promise<void>2122export interface BootstrapAppOptions {23  providers?: readonly ProviderInput[]24}2526export interface App extends AsyncDisposable {27  start(): Promise<void> // idempotent; rejects once the app is stopped28  stop(): Promise<void> // idempotent29}

Handle & Utility Types

Exported typeDefinition / shapePurpose
Token<T>Branded opaque interface carrying TContract key returned by token<T>().
Ref<T>extends Token<T> (branded as a ref)Self-providing handle returned by provide().
RefType<T>T extends Ref<infer U> ? U : neverExtracts the resolved type U from typeof someRef—great for typing test fakes without writing duplicate interfaces.
InjectKey<T>Token<T> | Ref<T>Any valid lookup or binding target accepted by inject() and provideFor().
ProviderDef<T>Branded opaque interface carrying TExplicit binding returned by provideFor().
ProviderBundleBranded opaque interfaceGrouped wiring handle returned by bundleProviders().
ProviderInputRef<unknown> | ProviderDef<unknown> | ProviderBundle | readonly ProviderInput[]Any installable item or arbitrarily nested array accepted by providers and bundleProviders().
ProviderOptions{ providers?: readonly ProviderInput[] }Options object for provide() and provideFor() declaring a private child subtree.
BootstrapAppOptions{ providers?: readonly ProviderInput[] }Options object for bootstrapApp() installing root-scope bindings.
InjectFn<T>(key: InjectKey<T>) => TType of ctx.inject passed inside Context.
OnDisposeFn(fn: () => void | Promise<void>) => voidType of ctx.onDispose passed inside Context.
Factory<T>(ctx: Context) => TProvider factory function signature.
BootstrapAppFn(ctx: Context) => void | Promise<void>Root startup callback signature for bootstrapApp().

Frozen Opaque Handle Semantics

Every handle returned by token(), provide(), provideFor(), and bundleProviders() is an empty frozen object (Object.freeze({})) with zero own enumerable properties and zero own symbol keys. All internal metadata is stored inside a private WeakMap inside the kernel:

  • Mutation fails: Attempting to attach or overwrite properties on a handle throws a TypeError in strict mode.
  • Clones and forgeries are rejected: Spreading or copying a handle ({ ...sampleRef }, Object.assign({}, myToken)) creates a brand-new object reference absent from the kernel's WeakMap. Passing that copy to isProvideRef() returns false; passing it to inject() or provideFor() throws Invalid inject key received; passing a copied binding or bundle in providers throws Invalid provider input received.

For every runtime error message the kernel can throw—and how to fix each one—flip to Errors & Diagnostics.

opaque-handles.ts
1export const sampleRef = provide(() => 'ok')2export const sampleToken = token<string>('Sample')3export const sampleBinding = provideFor(sampleToken, () => 'ok')4export const sampleBundle = bundleProviders(sampleRef, sampleBinding)56console.log(isProvideRef(sampleRef)) // true7console.log(isProvideRef(sampleToken)) // false8console.log(isProvideRef(sampleBinding)) // false9console.log(isProvideRef(sampleBundle)) // false10console.log(isProvideRef({ ...sampleRef })) // false (copied object is rejected)