Skip to content

Commit f77f51c

Browse files
committed
feat: type assertion
1 parent d85bcb9 commit f77f51c

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# 1.0.0 (2020-07-15)
1+
# 1.0.0 (2020-07-27)
22

33

44
### Bug Fixes
55

66
* monaco editor build bug (cannot require async component) ([906f6e8](https://github.com/Rain120/typescript-guide/commit/906f6e8379efe762d76f16381b657e4752be4dd4))
77
* monaco editor show normal modal and can edit ([da7a4c9](https://github.com/Rain120/typescript-guide/commit/da7a4c9316b6bf245ceccea93f3518ddaf810f80))
88
* nested sub-level directories path ([c6830ac](https://github.com/Rain120/typescript-guide/commit/c6830ac37ada02270874d8d3d7ab00fade02e51d))
9+
* README OKR path not found ([5d9739d](https://github.com/Rain120/typescript-guide/commit/5d9739d17602787fb8d1ad190d225fc572003920))
910
* update google analysis id ([2e1721b](https://github.com/Rain120/typescript-guide/commit/2e1721b7f5a7b671398f5d6ec2c094194c4229c0))
1011

1112

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Learning all about the base of Typescript, and practice it
3939

4040
- [x] [Basic](./docs/zh/basic/README.md)
4141

42-
- [ ] [Type Assertion](./docs/zh/type-assertion/README.md)
42+
- [x] [Type Assertion](./docs/zh/type-assertion/README.md)
4343

4444
- [ ] [Union Types](./docs/zh/union-types/README.md)
4545

docs/zh/guide/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Learning all about the base of Typescript, and practice it
4040

4141
- [x] [Basic](../basic/README.md)
4242

43-
- [ ] [Type Assertion](../type-assertion/README.md)
43+
- [x] [Type Assertion](../type-assertion/README.md)
4444

4545
- [ ] [Union Types](../union-types/README.md)
4646

docs/zh/type-assertion/README.md

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,88 @@
11
## 定义
22

3+
有时候你会比 `Typescript` 更了解你的值的类型, `Typescript` 允许你使用你想要的方式分析并覆盖它, 这种机制被称为 **「类型断言」**
4+
35
## 使用
46

7+
类型断言有 **两种形式**:
8+
9+
### as
10+
11+
```ts
12+
const someValue: any = "this is a string";
13+
14+
const strLength: number = (someValue as string).length;
15+
```
16+
17+
### <> 尖括号
18+
19+
```ts
20+
const someValue: any = "this is a string";
21+
22+
const strLength: number = (<string>someValue).length;
23+
```
24+
25+
**Note:** 当你在 **`.jsx`** `or` **`.tsx`** 中使用 **尖括号** 的方式进行类型断言时, 会造成 **语言歧义**, 为了保持一致性, 推荐使用 `as` 语法来进行 **类型断言**[相关体验](https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgApQPY2AGxQbwCgBIEOAWwgC5kBnMKUAcwG4S4nrkQBXcgI2jIAPnQbM2AX0KEEGEPWRweYABYYoyALzIAPOiy4IAPnyS2hZWo0A6MpW3IA5ACU4oAIwAmAAxPCQA)
26+
27+
## 双重断言
28+
29+
原理: 任何类型都可以被断言为 `any`, 而 `any` 可以被断言为任何类型。
30+
31+
```ts
32+
interface Person {
33+
name: string;
34+
}
35+
36+
const mine = 'Rain120';
37+
38+
((age as any) as Person).name;
39+
40+
```
41+
42+
**Note:** 双重断言极具破坏性, 而且它很可能会导致运行时错误, 慎用 !!!
43+
44+
## 类型断言 与 类型转换
45+
46+
在计算机科学中, **[类型转换 (type conversion)](https://zh.wikipedia.org/zh-cn/%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2)** 是指将数据从一种类型 **转换成** 另一种类型的过程。所以 **类型转换** 是在 **运行时** 转换的, 而 **类型断言** 只是一个编译时的语法, 为编译器提供关于如何分析代码的方法。
47+
48+
若要进行类型转换, 需要直接调用类型转换的方法:
49+
```ts
50+
Boolean();
51+
52+
String();
53+
54+
parseInt();
55+
56+
// etc...
57+
```
58+
59+
## 断言判断
60+
61+
- **C类型****P类型****子类型**
62+
63+
- **P类型****C类型****子类型**
64+
65+
**C类型** 能被断言为 **P类型**
66+
567
## 快来耍耍啊
668

769
### 🌰🌰
870

971
<!-- 题目 -->
1072

11-
```
12-
// template
73+
请解决👇👇👇报错
74+
75+
```ts
76+
interface Profile {
77+
name: string;
78+
age: number | string;
79+
}
80+
81+
const author = {};
82+
83+
// Property 'name' does not exist on type '{}'.(2339)
84+
author.name = 'Rain120';
85+
1386
```
1487

1588
### 游乐场
@@ -23,8 +96,24 @@
2396
### 参考答案
2497

2598
```ts
26-
// answer
99+
100+
interface Profile {
101+
name: string;
102+
age: number | string;
103+
}
104+
105+
const author = {} as Profile;
106+
107+
// const author = <Profile>{};
108+
109+
author.name = 'Rain120';
110+
27111
```
28112

29113
## 参考资料
30114

115+
[Handbook - type-assertion](https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions)
116+
117+
[深入理解 Typescript](https://jkchao.github.io/typescript-book-chinese/typings/typeAssertion.html)
118+
119+
[Typescript - type-assertion](https://ts.xcatliu.com/basics/type-assertion.html)

0 commit comments

Comments
 (0)