ioc.ts
TypeScript Dependency Injection
Features
- Type safe
- Tiny (614 bytes before minification or compression)
- No classes required
- Supports multiple containers
Install
npm i -S ioc-ts
API
Import:
import container from 'ioc-ts'
container(deps?: { [key: string]: any }): Container
Create a container, optionally specifying dependencies.
Dependencies should be an object. Returns the container.
Container#define(deps?: { [key: string]: any }): Container
Add additional dependencies to a container. Does not mutate underlying container. Can be chained.
// c now has x, y and z.
Container#combine(container: Container): Container
Combine the dependencies of two containers. Does not mutate underlying container. Can be chained.
// c now has x, y and z.
Container#inject(fn: Function): Function
Inject a function with dependencies from a container.
Container must type-match the dependencies of the target function.
Target function must specify dependencies as the first argument.
iHasDependencies42 // logs 'foo', 42