Skip to content

Commit b231564

Browse files
author
Umed Khudoiberdiev
committed
fixes #26
1 parent 9d2948c commit b231564

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typedi",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"description": "Dependency injection for TypeScript",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/decorators/Inject.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Container} from "../Container";
2+
import {Token} from "../Token";
23

34
/**
45
* Injects a service into a class property or constructor parameter.
@@ -13,7 +14,12 @@ export function Inject(serviceName?: string): Function;
1314
/**
1415
* Injects a service into a class property or constructor parameter.
1516
*/
16-
export function Inject(typeOrName?: ((type?: any) => Function)|string): Function {
17+
export function Inject(token: Token<any>): Function;
18+
19+
/**
20+
* Injects a service into a class property or constructor parameter.
21+
*/
22+
export function Inject(typeOrName?: ((type?: any) => Function)|string|Token<any>): Function {
1723
return function(target: Object, propertyName: string, index?: number) {
1824

1925
if (!typeOrName)
@@ -23,7 +29,19 @@ export function Inject(typeOrName?: ((type?: any) => Function)|string): Function
2329
object: target,
2430
propertyName: propertyName,
2531
index: index,
26-
value: () => Container.get<any>(typeof typeOrName === "string" ? typeOrName : typeOrName() as any)
32+
value: () => {
33+
let identifier: any;
34+
if (typeof typeOrName === "string") {
35+
identifier = typeOrName;
36+
37+
} else if (typeOrName instanceof Token) {
38+
identifier = typeOrName;
39+
40+
} else {
41+
identifier = typeOrName();
42+
}
43+
return Container.get<any>(identifier);
44+
}
2745
});
2846
};
2947
}

test/decorators/Inject.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import "reflect-metadata";
22
import {Container} from "../../src/Container";
33
import {Service} from "../../src/decorators/Service";
44
import {Inject} from "../../src/decorators/Inject";
5+
import {Token} from "../../src/Token";
56

67
describe("Inject Decorator", function() {
78

@@ -19,6 +20,23 @@ describe("Inject Decorator", function() {
1920
Container.get(SecondTestService).testService.should.be.instanceOf(TestService);
2021
});
2122

23+
it("should inject token service properly", function() {
24+
interface Test {
25+
26+
}
27+
const ServiceToken = new Token<Test>();
28+
29+
@Service(ServiceToken)
30+
class TestService {
31+
}
32+
@Service()
33+
class SecondTestService {
34+
@Inject(ServiceToken)
35+
testService: Test;
36+
}
37+
Container.get(SecondTestService).testService.should.be.instanceOf(TestService);
38+
});
39+
2240
it("should inject named service into class property", function() {
2341
@Service("mega.service")
2442
class NamedService {

0 commit comments

Comments
 (0)