Dependency Injection
Requires TypeScript 5.4 or newer.
npm i udic
bun i udic
pnpm i udic
yarn add udic
Guides
Section titled “Guides” Using Services Create and use services.
Provide implementations Provide implementations for computes.
Import:
import * as di from 'udic';
// Named importsimport { service, use, inject, impl, link} from 'udic';
// Type importsimport type { Service, Compute, Impl InferResult, InferDependency} from 'udic';
service()
Section titled “service()”<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.
inject()
Section titled “inject()”<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.