injecd
v0.6.0
Published
Dependency Injection, superlight with minimum boilerplate
Downloads
17
Maintainers
Readme
injecd
Dependency Injection made simple.
By leveraging default parameters, injecd minimizes the usual boilerplate present in TypeScript injection, while retaining type checking and automatic code completion!
installation
npm install injecd
importing
import { injecd, InjecdContainer } from "injecd";
Usage
1. Tag
const greeting$ = injecd<string>();
const class$ = injecd<A>();
2. Mark
class A {
constructor(public greeting = greeting$.r) {}
}
3. Spawn
const container = new InjecdContainer();
4. Register
container.registerInstance(greeting$, "Hello World!");
container.registerClass(class$, A);
5. Resolve!
const instance = container.resolve(class$);
console.log(instance.greeting); // > Hello World!
Tagging with type inferrence shorthands
Instead of injecd<typeof weird>()
:
const weird = { weird: "untyped", inferred: "object" };
const weird$ = injecd(weird):
Instead of injecd<ReturnType<typeof weirdFactory>>()
:
function weirdFactory() {
return { weird: "untyped", inferred: "object" };
}
const weird$ = injecdReturn(weirdFactory);
More information
For detailed usage examples refer to the GitHub README