TypeDI
Search…
@Inject decorator
The @Inject() decorator is a property and parameter decorator used to resolve dependencies on a property of a class or a constructor parameter. By default it infers the type of the property or argument and initializes an instance of the detected type, however, this behavior can be overwritten via specifying a custom constructable type, Token, or named service as the first parameter.

Property injection

This decorator is mandatory on properties where a class instance is desired (aka: without the decorator, the property will stay undefined). The type of the property is automatically inferred so there is no need to define the desired value as the decorator parameter.
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service()
5
class InjectedExampleClass {
6
print() {
7
console.log('I am alive!');
8
}
9
}
10
11
@Service()
12
class ExampleClass {
13
@Inject()
14
withDecorator: InjectedExampleClass;
15
16
withoutDecorator: InjectedExampleClass;
17
}
18
19
const instance = Container.get(ExampleClass);
20
21
/**
22
* The `instance` variable is an ExampleClass instance with the `withDecorator`
23
* property containing an InjectedExampleClass instance and `withoutDecorator`
24
* property being undefined.
25
*/
26
console.log(instance);
27
28
instance.withDecorator.print();
29
// prints "I am alive!" (InjectedExampleClass.print function)
30
console.log(instance.withoutDecorator);
31
// logs undefined, as this property was not marked with an @Inject decorator
Copied!

Constructor Injection

The @Inject decorator is not required in constructor injection when a class is marked with the @Service decorator. TypeDI will automatically infer and inject the correct class instances for every constructor argument. However, it can be used to overwrite the injected type.
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service()
5
class InjectedExampleClass {
6
print() {
7
console.log('I am alive!');
8
}
9
}
10
11
@Service()
12
class ExampleClass {
13
constructor(
14
@Inject()
15
public withDecorator: InjectedExampleClass,
16
public withoutDecorator: InjectedExampleClass
17
) {}
18
}
19
20
const instance = Container.get(ExampleClass);
21
22
/**
23
* The `instance` variable is an ExampleClass instance with both the
24
* `withDecorator` and `withoutDecorator` property containing an
25
* InjectedExampleClass instance.
26
*/
27
console.log(instance);
28
29
instance.withDecorator.print();
30
// prints "I am alive!" (InjectedExampleClass.print function)
31
instance.withoutDecorator.print();
32
// prints "I am alive!" (InjectedExampleClass.print function)
Copied!

Explicitly requesting target type

By default, TypeDI will try to infer the type of property and arguments and inject the proper class instance. When this is possible (eg: the property type is an interface) there is three-way to overwrite type of the injected value:
    via @Inject(() => type) where type is a constructable value (eg: a class definition)
    via @Inject(myToken) where myToken is an instance of Token class
    via @Inject(serviceName) where serviceName is a string which is already registered via Container.set(serviceName, value)
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service()
5
class InjectedExampleClass {
6
print() {
7
console.log('I am alive!');
8
}
9
}
10
11
@Service()
12
class BetterInjectedClass {
13
print() {
14
console.log('I am a different class!');
15
}
16
}
17
18
@Service()
19
class ExampleClass {
20
@Inject()
21
inferredPropertyInjection: InjectedExampleClass;
22
23
/**
24
* We tell TypeDI that initialize the `BetterInjectedClass` class
25
* regardless of what is the inferred type.
26
*/
27
@Inject(() => BetterInjectedClass)
28
explicitPropertyInjection: InjectedExampleClass;
29
30
constructor(
31
public inferredArgumentInjection: InjectedExampleClass,
32
/**
33
* We tell TypeDI that initialize the `BetterInjectedClass` class
34
* regardless of what is the inferred type.
35
*/
36
@Inject(() => BetterInjectedClass)
37
public explicitArgumentInjection: InjectedExampleClass
38
) {}
39
}
40
41
/**
42
* The `instance` variable is an ExampleClass instance with both the
43
* - `inferredPropertyInjection` and `inferredArgumentInjection` property
44
* containing an `InjectedExampleClass` instance
45
* - `explicitPropertyInjection` and `explicitArgumentInjection` property
46
* containing a `BetterInjectedClass` instance.
47
*/
48
const instance = Container.get(ExampleClass);
49
50
instance.inferredPropertyInjection.print();
51
// prints "I am alive!" (InjectedExampleClass.print function)
52
instance.explicitPropertyInjection.print();
53
// prints "I am a different class!" (BetterInjectedClass.print function)
54
instance.inferredArgumentInjection.print();
55
// prints "I am alive!" (InjectedExampleClass.print function)
56
instance.explicitArgumentInjection.print();
57
// prints "I am a different class!" (BetterInjectedClass.print function)
Copied!
Last modified 9d ago