https://github.com/imoverlord/sloth-ts-injection
Sloth Dependency Injection For Typescript
https://github.com/imoverlord/sloth-ts-injection
dependency dependency-injection injection sloth sloth-dependency-injection tool typescript
Last synced: about 2 months ago
JSON representation
Sloth Dependency Injection For Typescript
- Host: GitHub
- URL: https://github.com/imoverlord/sloth-ts-injection
- Owner: ImOverlord
- License: mit
- Created: 2019-01-23T21:23:25.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:36:33.000Z (over 3 years ago)
- Last Synced: 2025-05-24T05:20:31.549Z (about 1 year ago)
- Topics: dependency, dependency-injection, injection, sloth, sloth-dependency-injection, tool, typescript
- Language: TypeScript
- Size: 239 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sloth-ts-injection
[](https://travis-ci.org/ImOverlord/sloth-ts-injection)
[](https://coveralls.io/github/ImOverlord/sloth-ts-injection?branch=master)
Sloth Dependency Injection For Typescript
## Installation
You can get the latest release and the type definitions using npm:
```
$ npm install sloth-ts-injection reflect-metadata --save
```
> /!\\ sloth-ts-injection requires TypeScript >= 2.0, your tsconfig should look similar to the one below (experimentalDecorators, emitDecoratorMetadata are important)
```js
{
"compilerOptions": {
"target": "es5",
"lib": ["es6"],
"types": ["reflect-metadata"],
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
```
## Getting Started
You can declare dependencies with @slothInject() decorator.
```ts
import { slothInject } from 'sloth-ts-injection';
@slothInject()
export class DependencyA {
public data: number = 0;
}
```
You can then require this dependency.
```ts
import { slothInject } from 'sloth-ts-injection';
import { DependencyA } from "./dependencyA";
@slothInject()
export class DependencyD {
constructor(public a: DependencyA ) { }
public update() {
this.a.data++;
}
}
```
sloth-ts-injection will resolve the dependencies needed for you
```ts
import { Injector } from 'sloth-ts-injection';
const injector = new Injector();
const dep: DependencyD = injector.inject(DependencyD);
dep.update();
```