File tree Expand file tree Collapse file tree 3 files changed +37
-21
lines changed Expand file tree Collapse file tree 3 files changed +37
-21
lines changed Original file line number Diff line number Diff line change @@ -86,7 +86,6 @@ Container.set([
86
86
- [ Using factory function to create service] ( #using-factory-function-to-create-service )
87
87
- [ Using factory class to create service] ( #using-factory-class-to-create-service )
88
88
- [ Problem with circular references] ( #problem-with-circular-references )
89
- - [ Inherited injections] ( #inherited-injections )
90
89
- [ Custom decorators] ( #custom-decorators )
91
90
- [ Using service groups] ( #using-service-groups )
92
91
- [ Using multiple containers and scoped containers] ( #using-multiple-containers-and-scoped-containers )
@@ -184,26 +183,6 @@ export class Engine {
184
183
185
184
And that's all. Same for constructor injections.
186
185
187
- ### Inherited injections
188
-
189
- Inherited injections are supported as well. In order to use them you must mark inherited class as a @Service .
190
- For example:
191
-
192
- ``` typescript
193
- // Car.ts
194
- @Service ()
195
- export abstract class Car {
196
- @Inject ()
197
- engine: Engine ;
198
- }
199
-
200
- // Engine.ts
201
- @Service ()
202
- export class Bus extends Car {
203
- // you can call this.engine in this class
204
- }
205
- ```
206
-
207
186
### Custom decorators
208
187
209
188
You can create your own decorators which will inject your given values for your service dependencies.
Original file line number Diff line number Diff line change 8
8
- [ @Service decorator] ( typescript/04-service-decorator.md )
9
9
- [ @Inject decorator] ( typescript/05-inject-decorator.md )
10
10
- [ Service Tokens] ( typescript/06-service-tokens.md )
11
+ - [ Inheritance] ( typescript/07-inheritance.md )
11
12
- [ Usage with TypeORM] ( typescript/07-usage-with-typeorm.md )
12
13
- Advanced Usage
13
14
- [ Creating custom decorators] ( typescript/08-custom-decorators.md )
Original file line number Diff line number Diff line change
1
+ # Inheritance
2
+
3
+ Inheritance is supported ** for properties** when both the base and the extended class is marked with the ` @Service() ` decorator.
4
+ Classes which extends a class with decorated properties will receive the initialized class instances on those properties upon creation.
5
+
6
+ ``` ts
7
+ import ' reflect-metadata' ;
8
+ import { Container , Token , Inject , Service } from ' typedi' ;
9
+
10
+ @Service ()
11
+ class InjectedClass {
12
+ name: string = ' InjectedClass' ;
13
+ }
14
+
15
+ @Service ()
16
+ class BaseClass {
17
+ name: string = ' BaseClass' ;
18
+
19
+ @Inject ()
20
+ injectedClass: InjectedClass ;
21
+ }
22
+
23
+ @Service ()
24
+ class ExtendedClass extends BaseClass {
25
+ name: string = ' ExtendedClass' ;
26
+ }
27
+
28
+ const instance = Container .get (ExtendedClass );
29
+ // instance has the `name` property with "ExtendedClass" value (overwritten the base class)
30
+ // and the `injectedClass` property with the instance of the `InjectedClass` class
31
+
32
+ console .log (instance .injectedClass .name );
33
+ // logs "InjectedClass"
34
+ console .log (instance .name );
35
+ // logs "ExtendedClass"
36
+ ```
You can’t perform that action at this time.
0 commit comments