Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 029d0d6

Browse files
committed
Fix typo
1 parent b5d7a97 commit 029d0d6

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

basics/type-assertion.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function isFish(animal: Cat | Fish) {
6666
// Property 'swim' does not exist on type 'Cat'.
6767
```
6868

69-
上例中,获取 `animal.swim` 的时候会报错。
69+
上面的例子中,获取 `animal.swim` 的时候会报错。
7070

7171
此时可以使用类型断言,将 `animal` 断言成 `Fish`
7272

@@ -90,7 +90,7 @@ function isFish(animal: Cat | Fish) {
9090

9191
这样就可以解决访问 `animal.swim` 时报错的问题了。
9292

93-
需要注意的是,类型断言只能够「欺骗」TypeScript 编译器,无法避免运行时的错误,甚至与滥用类型断言可能会导致运行时错误
93+
需要注意的是,类型断言只能够「欺骗」TypeScript 编译器,无法避免运行时的错误,甚至于滥用类型断言可能会导致运行时错误
9494

9595
```ts
9696
interface Cat {
@@ -106,11 +106,11 @@ function swim(animal: Cat | Fish) {
106106
(animal as Fish).swim();
107107
}
108108

109-
const cat: Cat = {
109+
const tom: Cat = {
110110
name: 'Tom',
111-
run() { console.log('run'); }
111+
run() { console.log('run') }
112112
};
113-
swim(cat);
113+
swim(tom);
114114
```
115115

116116
上面的例子编译时不会报错,但在运行时会报错:
@@ -220,7 +220,7 @@ foo.length = 1;
220220
// index.ts:2:5 - error TS2339: Property 'length' does not exist on type 'number'.
221221
```
222222

223-
上例中,数字类型的变量 `foo` 上是没有 `length` 属性的,故 TypeScript 给出了相应的错误提示。
223+
上面的例子中,数字类型的变量 `foo` 上是没有 `length` 属性的,故 TypeScript 给出了相应的错误提示。
224224

225225
这种错误提示显然是非常有用的。
226226

@@ -232,7 +232,7 @@ window.foo = 1;
232232
// index.ts:1:8 - error TS2339: Property 'foo' does not exist on type 'Window & typeof globalThis'.
233233
```
234234

235-
上例中,我们需要将 `window` 上添加一个属性 `foo`,但 TypeScript 编译时会报错,提示我们 `window` 上不存在 `foo` 属性。
235+
上面的例子中,我们需要将 `window` 上添加一个属性 `foo`,但 TypeScript 编译时会报错,提示我们 `window` 上不存在 `foo` 属性。
236236

237237
此时我们可以使用 `as any` 临时将 `window` 断言为 `any` 类型:
238238

@@ -312,11 +312,11 @@ interface Cat {
312312
run(): void;
313313
}
314314

315-
let cat: Cat = {
315+
let tom: Cat = {
316316
name: 'Tom',
317317
run: () => { console.log('run') }
318318
};
319-
let animal: Animal = cat;
319+
let animal: Animal = tom;
320320
```
321321

322322
我们知道,TypeScript 是结构类型系统,类型之间的对比只会比较它们最终的结构,而会忽略它们定义时的关系。
@@ -332,7 +332,7 @@ interface Cat extends Animal {
332332
}
333333
```
334334

335-
那么也不难理解为什么 `Cat` 类型的 `cat` 可以赋值给 `Animal` 类型的 `animal` 了——就像面向对象编程中我们可以将子类的实例赋值给类型为父类的变量。
335+
那么也不难理解为什么 `Cat` 类型的 `tom` 可以赋值给 `Animal` 类型的 `animal` 了——就像面向对象编程中我们可以将子类的实例赋值给类型为父类的变量。
336336

337337
我们把它换成 TypeScript 中更专业的说法,即:`Animal` 兼容 `Cat`
338338

@@ -378,7 +378,7 @@ function testCat(cat: Cat) {
378378
- any 可以被断言为任何类型
379379
- 要使得 `A` 能够被断言为 `B`,只需要 `A` 兼容 `B``B` 兼容 `A` 即可
380380

381-
其实前两种情况都是最后一个的特例
381+
其实前四种情况都是最后一个的特例
382382

383383
## 双重断言
384384

@@ -556,7 +556,7 @@ const tom: Cat = getCacheData('tom');
556556

557557
> 本小结的前置知识点:[范型][]
558558
559-
在这个例子中
559+
还是这个例子
560560

561561
```ts
562562
function getCacheData(key: string): any {

0 commit comments

Comments
 (0)