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

Skip to content

Commit 9cf37a2

Browse files
committed
Fix typo
1 parent 029d0d6 commit 9cf37a2

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

basics/type-assertion.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function isFish(animal: Cat | Fish) {
9090

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

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

9595
```ts
9696
interface Cat {
@@ -111,6 +111,7 @@ const tom: Cat = {
111111
run() { console.log('run') }
112112
};
113113
swim(tom);
114+
// Uncaught TypeError: animal.swim is not a function`
114115
```
115116

116117
上面的例子编译时不会报错,但在运行时会报错:
@@ -404,7 +405,7 @@ function testCat(cat: Cat) {
404405

405406
在上面的例子中,若直接使用 `cat as Fish` 肯定会报错,因为 `Cat``Fish` 互相都不兼容。
406407

407-
但是若使用双重断言,则可以打破「要使得 `A` 能够被断言为 `B`,只需要 `A` 兼容 `B``B` 兼容 `A` 即可」的规则,将任何一个类型断言为任何另一个类型。
408+
但是若使用双重断言,则可以打破「要使得 `A` 能够被断言为 `B`,只需要 `A` 兼容 `B``B` 兼容 `A` 即可」的限制,将任何一个类型断言为任何另一个类型。
408409

409410
若你使用了这种双重断言,那么十有八九是非常错误的,它很可能会导致运行时错误。
410411

@@ -483,7 +484,7 @@ const tom: Cat = getCacheData('tom');
483484
tom.run();
484485
```
485486

486-
上面的例子中,我们通过类型声明的方式,将 `tom` 声明为 `Cat`,然后再将 `getCacheData('tom')` 赋值给类型为 `Cat` `tom`
487+
上面的例子中,我们通过类型声明的方式,将 `tom` 声明为 `Cat`,然后再将 `any` 类型的 `getCacheData('tom')` 赋值给 `Cat` 类型的 `tom`
487488

488489
这和类型断言是非常相似的,而且产生的结果也几乎是一样的——`tom` 在接下来的代码中都变成了 `Cat` 类型。
489490

0 commit comments

Comments
 (0)