Installation
npm i @ylcnfrht/iocpnpm add @ylcnfrht/iocyarn add @ylcnfrht/iocbun add @ylcnfrht/iocThe package has no runtime dependencies. It needs Node.js 20 or later; any modern browser or bundler works too.
Importing
Section titled “Importing”Both module formats are published, each with its own type declarations.
import { ServiceCollection, createToken } from '@ylcnfrht/ioc';const { ServiceCollection, createToken } = require('@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.
TypeScript settings
Section titled “TypeScript settings”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,emitDecoratorMetadataorreflect-metadata. Dependencies are listed at registration, not read from metadata. strictrecommended. 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 compilesawait usingdown to a helper that needsSymbol.asyncDisposeat runtime, which Node.js defines from 20.4 on; recent Node.js versions also runawait usingnatively.
{ "compilerOptions": { "target": "ES2022", "lib": ["ES2022", "ESNext.Disposable"], "module": "NodeNext", "strict": true }}JavaScript
Section titled “JavaScript”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.