Skip to content

Dependency Injection

Requires TypeScript 5.4 or newer.

Terminal window
npm i udic

Import:

import * as di from 'udic';
// Named imports
import {
service, use, inject,
impl, link
} from 'udic';
// Type imports
import type {
Service, Compute, Impl
InferResult, InferDependency
} from 'udic';
  • <T> - Service identifier type.
  • name: T - Service identifier.
  • Returns <K>() => di.Service<T, K> & K

Create a service.

  • <const T extends di.Dependency[]> - Compute dependency list type.
  • <const R> - Compute result type.
  • deps: T - Compute dependency list.
  • f: (...args: { [K in keyof T] : di.InferResult<T[K]> }) => R - The function to compute the result from provided dependencies.
  • Returns di.Compute<di.InferDependency<T[number]>, R> - A function that requires the implementations of the provided dependencies to execute.

Create a compute that requires the specified dependencies to run.

  • <T> - Input compute dependencies type.
  • <R> - Input compute result type.
  • <D extends Partial<T>> - Provided dependencies type.
  • compute: di.Compute<T, R> - Target compute to inject dependencies.
  • deps: D - Dependencies to inject to the compute.
  • Returns di.Compute<di.Prettify<Omit<T, keyof D>>, R>

Create a new compute with provided dependencies.