Skip to content

Commit a57c6e6

Browse files
authored
Update inheritance.md
Fix code formatting
1 parent dd39e31 commit a57c6e6

File tree

1 file changed

+53
-56
lines changed

1 file changed

+53
-56
lines changed

Diff for: wiki/inheritance.md

+53-56
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ We try to provide developers with useful error feedback like:
44

55
> Error: Missing required @injectable annotation in: SamuraiMaster
66
7-
This works fine in most cases but it causes some problem when using inheritance.
7+
This works fine in most cases but it causes some problem when using inheritance.
88

99
For example, the following code snippet throws a misleading error:
1010

@@ -20,9 +20,9 @@ class Warrior {
2020
}
2121

2222
@injectable()
23-
class SamuraiMaster extends Warrior {
23+
class SamuraiMaster extends Warrior {
2424
public constructor() { // args count = 0
25-
super("master");
25+
super("master");
2626
}
2727
}
2828
```
@@ -39,18 +39,17 @@ If you don't follow this rule an exception will be thrown:
3939
4040
The users have a few ways to overcome this limitation available:
4141

42-
### WORKAROUND A) Use the @unmanaged decorator
42+
### WORKAROUND A) Use the @unmanaged decorator
43+
4344
The `@unmanaged()` decorator allow users to flag that an argument will
44-
be manually injected into a base class. We use the word "unmanaged"
45-
because InversifyJS does not have control under user provided values
45+
be manually injected into a base class. We use the word "unmanaged"
46+
because InversifyJS does not have control under user provided values
4647
and it doesn't manage their injection.
4748

4849
The following code snippet showcases how to apply this decorator:
4950

5051
```ts
51-
import {
52-
Container, injectable, unmanaged
53-
} from "../src/inversify";
52+
import { Container, injectable, unmanaged } from "../src/inversify";
5453

5554
const BaseId = "Base";
5655

@@ -78,7 +77,7 @@ derived.prop; // "unmanaged-injected-value"
7877

7978
### WORKAROUND B) Property setter
8079

81-
You can use the `public`, `protected` or `private` access modifier and a
80+
You can use the `public`, `protected` or `private` access modifier and a
8281
property setter to avoid injecting into the base class:
8382

8483
```ts
@@ -93,8 +92,8 @@ class Warrior {
9392
@injectable()
9493
class SamuraiMaster extends Warrior {
9594
public constructor() { // args count = 0
96-
super();
97-
this.rank = "master";
95+
super();
96+
this.rank = "master";
9897
}
9998
}
10099
```
@@ -113,26 +112,26 @@ class Warrior {
113112
let TYPES = { Rank: "Rank" };
114113

115114
@injectable()
116-
class SamuraiMaster extends Warrior {
117-
115+
class SamuraiMaster extends Warrior {
118116
@injectNamed(TYPES.Rank, "master")
119117
@named("master")
120118
protected rank: string;
121-
119+
122120
public constructor() { // args count = 0
123-
super();
121+
super();
124122
}
125123
}
126124

127-
container.bind<string>(TYPES.Rank)
128-
.toConstantValue("master")
129-
.whenTargetNamed("master");
125+
container
126+
.bind<string>(TYPES.Rank)
127+
.toConstantValue("master")
128+
.whenTargetNamed("master");
130129
```
131130

132131
### WORKAROUND D) Inject into the derived class
133132

134-
If we don't want to avoid injecting into the base class we can
135-
inject into the derived class and then into the base class using
133+
If we don't want to avoid injecting into the base class we can
134+
inject into the derived class and then into the base class using
136135
its constructor (super).
137136

138137
```ts
@@ -147,17 +146,18 @@ class Warrior {
147146
let TYPES = { Rank: "Rank" };
148147

149148
@injectable()
150-
class SamuraiMaster extends Warrior {
149+
class SamuraiMaster extends Warrior {
151150
public constructor(
152-
@inject(TYPES.Rank) @named("master") rank: string // args count = 1
153-
) {
154-
super(rank);
151+
@inject(TYPES.Rank) @named("master") rank: string // args count = 1
152+
) {
153+
super(rank);
155154
}
156155
}
157156

158-
container.bind<string>(TYPES.Rank)
159-
.toConstantValue("master")
160-
.whenTargetNamed("master");
157+
container
158+
.bind<string>(TYPES.Rank)
159+
.toConstantValue("master")
160+
.whenTargetNamed("master");
161161
```
162162

163163
The following should also work:
@@ -172,40 +172,40 @@ class Warrior {
172172
}
173173

174174
interface Weapon {
175-
name: string;
175+
name: string;
176176
}
177177

178178
@injectable()
179179
class Katana implements Weapon {
180-
public name: string;
181-
public constructor() {
182-
this.name = "Katana";
183-
}
180+
public name: string;
181+
public constructor() {
182+
this.name = "Katana";
183+
}
184184
}
185185

186-
let TYPES = {
186+
let TYPES = {
187187
Rank: "Rank",
188-
Weapon: "Weapon"
188+
Weapon: "Weapon",
189189
};
190190

191191
@injectable()
192-
class SamuraiMaster extends Warrior {
193-
public weapon: Weapon;
192+
class SamuraiMaster extends Warrior {
193+
public weapon: Weapon;
194194
public constructor(
195-
@inject(TYPES.Rank) @named("master") rank: string, // args count = 2
196-
@inject(TYPES.Weapon) weapon: Weapon
197-
) {
198-
super(rank);
199-
this.weapon = weapon;
195+
@inject(TYPES.Rank) @named("master") rank: string, // args count = 2
196+
@inject(TYPES.Weapon) weapon: Weapon
197+
) {
198+
super(rank);
199+
this.weapon = weapon;
200200
}
201201
}
202202

203203
container.bind<Weapon>(TYPES.Weapon).to(Katana);
204204

205-
container.bind<string>(TYPES.Rank)
206-
.toConstantValue("master")
207-
.whenTargetNamed("master");
208-
205+
container
206+
.bind<string>(TYPES.Rank)
207+
.toConstantValue("master")
208+
.whenTargetNamed("master");
209209
```
210210

211211
### WORKAROUND E) Skip Base class `@injectable` checks
@@ -215,29 +215,26 @@ Setting the `skipBaseClassChecks` option to `true` for the container will disabl
215215
```ts
216216
// Not injectable
217217
class UnmanagedBase {
218-
public constructor(
219-
public unmanagedDependency: string
220-
) {
221-
}
218+
public constructor(public unmanagedDependency: string) {}
222219
}
223220

224221
@injectable()
225-
class InjectableDerived extends UnmanagedBase {
226-
public constructor(
227-
// Any arguments defined here will be injected like normal
228-
) {
222+
class InjectableDerived extends UnmanagedBase {
223+
public constructor() // Any arguments defined here will be injected like normal
224+
{
229225
super("Don't forget me...");
230226
}
231227
}
232228

233-
const container = new Container({skipBaseClassChecks: true});
229+
const container = new Container({ skipBaseClassChecks: true });
234230
container.bind(InjectableDerived).toSelf();
235231
```
236232

237233
This will work, and you'll be able to use your `InjectableDerived` class just like normal, including injecting dependencies from elsewhere in the container through the constructor. The one caveat is that you must make sure your `UnmanagedBase` receives the correct arguments.
238234

239235
## What can I do when my base class is provided by a third party module?
240-
In some cases, you may get errors about missing annotations in classes
236+
237+
In some cases, you may get errors about missing annotations in classes
241238
provided by a third party module like:
242239

243240
> Error: Missing required @injectable annotation in: SamuraiMaster

0 commit comments

Comments
 (0)