TypeDI
Search…
Usage Guide
IMPORTANT NOTE: Don't forget to annotate your classes with the @Service decorator! Both the ones being injected and those which requests the dependencies should be annotated.

Registering dependencies

There are three ways to register your dependencies:
  • annotating a class with the @Service() decorator (documentation)
  • registering a value with a Token
  • registering a value with a string identifier
The Token and string identifier can be used to register other values than classes. Both tokens and string identifiers can register any type of value including primitive values except undefined. They must be set on the container with the Container.set() function before they can be requested via Container.get().
1
import 'reflect-metadata';
2
import { Container, Inject, Service, Token } from 'typedi';
3
4
const myToken = new Token('SECRET_VALUE_KEY');
5
6
Container.set(myToken, 'my-secret-value');
7
Container.set('my-config-key', 'value-for-config-key');
8
Container.set('default-pagination', 30);
9
10
// somewhere else in your application
11
const tokenValue = Container.get(myToken);
12
const configValue = Container.get('my-config-key');
13
const defaultPagination = Container.get('default-pagination');
Copied!
For detailed documentation about @Service decorator please read the @Service decorator page.

Injecting dependencies

There are three ways to inject your dependencies:
  • automatic class constructor parameter injection
  • annotating class properties with the @Inject() decorator
  • directly using Container.get() to request an instance of a class, Token or string identifier

Constructor argument injection

Any class which has been marked with the @Service() decorator will have it's constructor properties automatically injected with the correct dependency.
TypeDI inserts the container instance which was used to resolve the dependencies as the last parameter in the constructor.
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service()
5
class InjectedClass {}
6
7
@Service()
8
class ExampleClass {
9
constructor(public injectedClass: InjectedClass) {}
10
}
11
12
const instance = Container.get(ExampleClass);
13
14
console.log(instance.injectedClass instanceof InjectedClass);
15
// prints true as TypeDI assigned the instance of InjectedClass to the property
Copied!

Property injection

Any property which has been marked with the @Inject decorator will be automatically assigned the instance of the class when the parent class is initialized by TypeDI.
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service()
5
class InjectedClass {}
6
7
@Service()
8
class ExampleClass {
9
@Inject()
10
injectedClass: InjectedClass;
11
}
12
13
const instance = Container.get(ExampleClass);
14
15
console.log(instance.injectedClass instanceof InjectedClass);
16
// prints true as the instance of InjectedClass has been assigned to the `injectedClass` property by TypeDI
Copied!
For detailed documentation about @Inject decorator please read the @Inject decorator page.

Using Container.get()

The Container.get() function can be used directly to request an instance of the target type. TypeDI will resolve and initialize all dependency on the target class. Container.get() can be used to request:
  • a constructable value (class definition) which will return the class instance
  • a Token which will return the value registered for that Token
  • a string which will return the value registered with that name
1
import 'reflect-metadata';
2
import { Container, Inject, Service, Token } from 'typedi';
3
4
const myToken = new Token('SECRET_VALUE_KEY');
5
6
@Service()
7
class InjectedClass {}
8
9
@Service()
10
class ExampleClass {
11
@Inject()
12
injectedClass: InjectedClass;
13
}
14
15
/** Tokens must be explicity set in the Container with the desired value. */
16
Container.set(myToken, 'my-secret-value');
17
/** String identifier must be explicity set in the Container with the desired value. */
18
Container.set('my-dependency-name-A', InjectedClass);
19
Container.set('my-dependency-name-B', 'primitive-value');
20
21
const injectedClassInstance = Container.get(InjectedClass);
22
// a class without dependencies can be required
23
const exampleClassInstance = Container.get(ExampleClass);
24
// a class with dependencies can be required and dependencies will be resolved
25
const tokenValue = Container.get(myToken);
26
// tokenValue will be 'my-secret-value'
27
const stringIdentifierValueA = Container.get('my-dependency-name-A');
28
// stringIdentifierValueA will be instance of InjectedClass
29
const stringIdentifierValueB = Container.get('my-dependency-name-B');
30
// stringIdentifierValueB will be 'primitive-value'
Copied!
For detailed documentation about Token class please read the Service Tokens page.

Singleton vs transient classes

Every registered service by default is a singleton. Meaning repeated calls to Container.get(MyClass) will return the same instance. If this is not the desired behavior a class can be marked as transient via the @Service() decorator.
1
import 'reflect-metadata';
2
import { Container, Inject, Service } from 'typedi';
3
4
@Service({ transient: true })
5
class ExampleTransientClass {
6
constructor() {
7
console.log('I am being created!');
8
// this line will be printed twice
9
}
10
}
11
12
const instanceA = Container.get(ExampleTransientClass);
13
const instanceB = Container.get(ExampleTransientClass);
14
15
console.log(instanceA !== instanceB);
16
// prints true
Copied!
Last modified 1mo ago