Skip to content

Commit 8d93b2d

Browse files
docs: add documentation for inheritance
1 parent 1520ece commit 8d93b2d

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

docs/README.md

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ Container.set([
8686
- [Using factory function to create service](#using-factory-function-to-create-service)
8787
- [Using factory class to create service](#using-factory-class-to-create-service)
8888
- [Problem with circular references](#problem-with-circular-references)
89-
- [Inherited injections](#inherited-injections)
9089
- [Custom decorators](#custom-decorators)
9190
- [Using service groups](#using-service-groups)
9291
- [Using multiple containers and scoped containers](#using-multiple-containers-and-scoped-containers)
@@ -184,26 +183,6 @@ export class Engine {
184183

185184
And that's all. Same for constructor injections.
186185

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-
207186
### Custom decorators
208187

209188
You can create your own decorators which will inject your given values for your service dependencies.

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [@Service decorator](typescript/04-service-decorator.md)
99
- [@Inject decorator](typescript/05-inject-decorator.md)
1010
- [Service Tokens](typescript/06-service-tokens.md)
11+
- [Inheritance](typescript/07-inheritance.md)
1112
- [Usage with TypeORM](typescript/07-usage-with-typeorm.md)
1213
- Advanced Usage
1314
- [Creating custom decorators](typescript/08-custom-decorators.md)

docs/typescript/07-inheritance.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
```

0 commit comments

Comments
 (0)