Skip to content

Commit 8c7d27a

Browse files
committed
feat:类型保护
1 parent 682a8c7 commit 8c7d27a

File tree

5 files changed

+133
-4
lines changed

5 files changed

+133
-4
lines changed

docs/typescript/generic.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 泛型
3-
order: 7
3+
order: 8
44
toc: menu
55
nav:
66
title: TypeScript
@@ -141,4 +141,4 @@ p.addMoney(1);
141141
p.addMoney(2);
142142

143143
p.getMoney();
144-
```
144+
```

docs/typescript/infer.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ let color: Color;
4646
color = 'red';
4747
color = 'blue'; // 错误
4848
```
49+
## 其他
50+
```javascript
51+
// 索引访问操作符
52+
interface IPerson {
53+
name: 'zf',
54+
age: number
55+
}
56+
57+
type P1= IPerson['age']
58+
59+
// 使用typeof反推变量类型
60+
let person = { username: 1 };
61+
type P2 = typeof person;
62+
```

docs/typescript/interface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 接口
3-
order: 6
3+
order: 7
44
toc: menu
55
nav:
66
title: TypeScript

docs/typescript/type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function fn(): void {
140140
没有类型是`never`的子类型或可以赋值给`never`类型(除了`never`本身之外)
141141

142142
```javascript
143-
let vv: never = any; // 错误
143+
let vv: never = 123; // 错误
144144

145145
// 永远达不到的情况有三种:错误 | 死循环 | 类型判断时出现never
146146
function ErrorHandler(msg: string): never {

docs/typescript/typeGuide.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: 类型守卫
3+
order: 9
4+
toc: menu
5+
nav:
6+
title: TypeScript
7+
---
8+
9+
类型守卫:通过判断识别所执行的代码块缩小类型的范围
10+
11+
## typeof
12+
```javascript
13+
function getValue(val: number | string) {
14+
if (typeof val === 'number') {
15+
val.toFixed(2);
16+
} else {
17+
val.toUpperCase();
18+
}
19+
}
20+
```
21+
22+
## instanceof
23+
```javascript
24+
class Person {
25+
username = 'rain';
26+
age = 11;
27+
}
28+
29+
class Animal {
30+
name = 'hj';
31+
}
32+
33+
function getName(instance: Person | Animal) {
34+
if (instance instanceof Person) {
35+
console.log(instance.username);
36+
}
37+
38+
if (instance instanceof Animal) {
39+
console.log(instance.name);
40+
41+
}
42+
}
43+
```
44+
## in
45+
`x in y` 表示`x`属性在`y`中存在
46+
```javascript
47+
class Person {
48+
username = 'rain';
49+
sayHello() {}
50+
}
51+
52+
class Animal {
53+
name = 'hj';
54+
eat() {}
55+
}
56+
57+
function getName(instance: Person | Animal) {
58+
if ('sayHello' in instance) {
59+
instance.sayHello()
60+
}
61+
62+
if ('eat' in instance) {
63+
instance.eat()
64+
}
65+
}
66+
```
67+
以上属于`javascript``typescript`共有的,下面介绍`typescript`中特有的
68+
69+
---
70+
71+
## 可辨识联合类型
72+
```javascript
73+
interface IBook1 {
74+
ID: 'XXX',
75+
used(flag: boolean): boolean
76+
}
77+
78+
interface IBook2 {
79+
ID: 'YYY',
80+
used(flag: boolean): boolean
81+
}
82+
83+
function getBook(book: IBook1 | IBook2) {
84+
if (book.ID === 'XXX') { // ID字段每个book不同,可辨识
85+
book.used(true);
86+
}
87+
88+
if (book.ID === 'YYY') {
89+
book.used(false)
90+
}
91+
}
92+
```
93+
## is
94+
95+
在vue源码中常用`is`来封装类型判断函数,比如:`isObject``isVNode`
96+
97+
```javascript
98+
// error: 虽然我们知道在if判断后val一定是string,但是ts不知道
99+
// ts会提示val可能是number类型, 不能执行toUpperCase方法
100+
101+
// const isString = (val: any): boolean => {
102+
// return typeof val === 'string'
103+
// }
104+
105+
// 如果返回值是true,则val is string
106+
const isString = (val: any): val is string => {
107+
return typeof val === 'string'
108+
}
109+
110+
let val: number | string = Math.random() > 0.5 ? 1 : '1';
111+
112+
if (isString(val)) {
113+
val.toUpperCase();
114+
}
115+
```

0 commit comments

Comments
 (0)