Skip to content

Commit 3f90151

Browse files
committed
2 parents bde2ffc + 71f2720 commit 3f90151

File tree

4 files changed

+17
-34
lines changed

4 files changed

+17
-34
lines changed

pages/Advanced Types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ let carProps: keyof Car; // the union of ('manufacturer' | 'model' | 'year')
813813
```
814814

815815
`keyof Car` is completely interchangeable with `'manufacturer' | 'model' | 'year'`.
816-
The difference is that if you add another property to `Car`, say `ownersAddress: string`, then `keyof Person` will automatically update to be `'manufacturer' | 'model' | 'year' | 'ownersAddress'`.
816+
The difference is that if you add another property to `Car`, say `ownersAddress: string`, then `keyof Car` will automatically update to be `'manufacturer' | 'model' | 'year' | 'ownersAddress'`.
817817
And you can use `keyof` in generic contexts like `pluck`, where you can't possibly know the property names ahead of time.
818818
That means the compiler will check that you pass the right set of property names to `pluck`:
819819

pages/Compiler Options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,5 @@ Option | Type | Default
9898

9999
## Related
100100

101-
* Setting compiler options in [`tsconfig.json`](./tsconfig.json.md) files.
101+
* Setting compiler options in [`tsconfig.json`](./tutorials/tsconfig.json.md) files.
102102
* Setting compiler options in [MSBuild projects](./Compiler%20Options%20in%20MSBuild.md).

pages/Enums.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ In other words, the following isn't allowed:
5858
```ts
5959
enum E {
6060
A = getSomeValue(),
61-
B, // error! 'A' is not constant-initialized, so 'B' needs an initializer
61+
B, // Error! Enum member must have initializer.
6262
}
6363
```
6464

@@ -174,8 +174,7 @@ interface Square {
174174
}
175175

176176
let c: Circle = {
177-
kind: ShapeKind.Square,
178-
// ~~~~~~~~~~~~~~~~ Error!
177+
kind: ShapeKind.Square, // Error! Type 'ShapeKind.Square' is not assignable to type 'ShapeKind.Circle'.
179178
radius: 100,
180179
}
181180
```
@@ -194,7 +193,7 @@ enum E {
194193
function f(x: E) {
195194
if (x !== E.Foo || x !== E.Bar) {
196195
// ~~~~~~~~~~~
197-
// Error! Operator '!==' cannot be applied to types 'E.Foo' and 'E.Bar'.
196+
// Error! This condition will always return 'true' since the types 'E.Foo' and 'E.Bar' have no overlap.
198197
}
199198
}
200199
```

pages/Mixins.md

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,17 @@ class Activatable {
3737
}
3838
}
3939

40-
class SmartObject implements Disposable, Activatable {
40+
class SmartObject {
4141
constructor() {
4242
setInterval(() => console.log(this.isActive + " : " + this.isDisposed), 500);
4343
}
4444

4545
interact() {
4646
this.activate();
4747
}
48-
49-
// Disposable
50-
isDisposed: boolean = false;
51-
dispose: () => void;
52-
// Activatable
53-
isActive: boolean = false;
54-
activate: () => void;
55-
deactivate: () => void;
5648
}
49+
50+
interface SmartObject extends Disposable, Activatable {}
5751
applyMixins(SmartObject, [Disposable, Activatable]);
5852

5953
let smartObj = new SmartObject();
@@ -105,29 +99,19 @@ Next, we'll create the class that will handle the combination of the two mixins.
10599
Let's look at this in more detail to see how it does this:
106100

107101
```ts
108-
class SmartObject implements Disposable, Activatable {
109-
```
102+
class SmartObject {
103+
...
104+
}
110105

111-
The first thing you may notice in the above is that instead of using `extends`, we use `implements`.
112-
This treats the classes as interfaces, and only uses the types behind Disposable and Activatable rather than the implementation.
113-
This means that we'll have to provide the implementation in class.
114-
Except, that's exactly what we want to avoid by using mixins.
106+
interface SmartObject extends Disposable, Activatable {}
107+
```
115108

116-
To satisfy this requirement, we create stand-in properties and their types for the members that will come from our mixins.
117-
This satisfies the compiler that these members will be available at runtime.
118-
This lets us still get the benefit of the mixins, albeit with some bookkeeping overhead.
109+
The first thing you may notice in the above is that instead of trying to extend `Disposable` and `Activatable` in `SmartObject` class, we extend them in `SmartObject` interface. `SmartObject` interface will be mixed into the `SmartObject` class due to the [declaration merging](./Declaration%20Merging.md).
119110

120-
```ts
121-
// Disposable
122-
isDisposed: boolean = false;
123-
dispose: () => void;
124-
// Activatable
125-
isActive: boolean = false;
126-
activate: () => void;
127-
deactivate: () => void;
128-
```
111+
This treats the classes as interfaces, and only mixes the types behind Disposable and Activatable into the SmartObject type rather than the implementation. This means that we'll have to provide the implementation in class.
112+
Except, that's exactly what we want to avoid by using mixins.
129113

130-
Finally, we mix our mixins into the class, creating the full implementation.
114+
Finally, we mix our mixins into the class implementation.
131115

132116
```ts
133117
applyMixins(SmartObject, [Disposable, Activatable]);

0 commit comments

Comments
 (0)