Lightweight wrap of di.js for TypeScript.
npm install qs-di-ts
You need to use
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
when compiling typescript files.
import {Inject} from 'qs-di-ts'
import {Engine} from './engine'
@Inject
export class Car {
constructor(public engine: Engine) {}
}
export class Engine {
}
import {Provide} from 'qs-di-ts'
import {Engine} from './engine'
@Provide(Engine)
export class MockEngine {
}
import {Injector} from 'qs-di-ts'
import {Car} from './car'
import {Engine} from './engine'
var injector = new Injector();
var car: Car = injector.get(Car); //instantiate car, car.engine is magically instance of Engine! :)
import {MockEngine} from './mockEngine'
var injector2 = new Injector([MockEngine]);
var car2: Car = injector2.get(Car); //instantiate car, car.engine is instance of MockEngine! :)
MIT