Skip to content
Buy me a coffee

Installation

Terminal window
npm i @ylcnfrht/ioc

The package has no runtime dependencies. It needs Node.js 20 or later; any modern browser or bundler works too.

Both module formats are published, each with its own type declarations.

import { ServiceCollection, createToken } from '@ylcnfrht/ioc';

There is one entry point. Everything is a named export: ServiceCollection, ServiceProvider, createToken, InjectionToken, ServiceLifetime, tokenName, the error classes and the public types.

Nothing special is required beyond a recent compiler:

  • TypeScript 5.4 or later. Implementations, factories and values are checked against the token’s type with NoInfer, which arrived in TypeScript 5.4. Plain JavaScript has no such requirement.
  • No decorators. You don’t need experimentalDecorators, emitDecoratorMetadata or reflect-metadata. Dependencies are listed at registration, not read from metadata.
  • strict recommended. The dependency checks compare constructor parameter types with token types; they are most useful with strict null checks on.
  • await using (to dispose a scope automatically) needs TypeScript 5.2 or later and the disposable types: set "lib" to include "ESNext.Disposable" (or "ESNext"). TypeScript compiles await using down to a helper that needs Symbol.asyncDispose at runtime, which Node.js defines from 20.4 on; recent Node.js versions also run await using natively.
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "ESNext.Disposable"],
"module": "NodeNext",
"strict": true
}
}

The library works from plain JavaScript too. You lose the compile-time checks, but the runtime checks stay: the dependency count is validated at registration and missing services throw typed errors. See Using it from JavaScript.