Skip to content

Commit c774615

Browse files
authored
Merge pull request #17 from FrontEndDev-org/docs
readme优化
2 parents 0eac1b6 + 42ae44b commit c774615

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

README.md

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,72 @@
1313

1414
## try-catch 块级作用域带来的问题
1515

16+
先看一段代码:
17+
1618
```ts
19+
const somePromise = Promise.resolve({ prop: 'value' });
20+
21+
try {
22+
// 块级作用域内赋值
23+
// res类型推断为 {prop: string}
24+
const res = await somePromise;
25+
// 类型安全地使用 res
26+
console.log(res.prop); // 'value'
27+
} catch (err) {
28+
// 此处 err 类型为 unknown
29+
console.log(err);
30+
}
31+
```
32+
33+
但有些时候,我们需要将变量的声明提升到块级作用域外,比如:
34+
35+
```ts
36+
const somePromise = Promise.resolve({ prop: 'value' });
37+
1738
// 需要先在 try-catch 块级作用域外定义变量,此处还需要先声明类型
39+
// 由于只提升了声明,但没有提升赋值,需要捕捉期望的返回值类型,并联合 undefined
40+
type Result = typeof somePromise extends Promise<infer T> ? T : never;
1841
let res: Result | undefined;
1942

2043
try {
2144
// 块级作用域内赋值
2245
res = await somePromise;
46+
// 块级作用域内类型仍然安全
47+
console.log(res.prop); // 'value'
2348
} catch (err) {
2449
// 此处 err 类型为 unknown
2550
console.log(err);
26-
return;
2751
}
2852

53+
// 其他操作...
54+
2955
// try-catch 块级作用域外使用该变量
30-
// 因为 res 类型包含 undefined,所以还要加有值判断
56+
// 此处 res 类型包含 undefined,类型使用不安全
57+
console.log(res.prop); // TS18048: 'res' is possibly 'undefined'.
58+
// 所以还要加有值判断
3159
if (res) {
3260
console.log(res.prop);
3361
}
3462
```
3563

64+
可以看到,由于块级作用域的特性,导致 res 的类型被”污染“了, 使用 try-flatten 后,你将可以用一种“扁平化”的方式调用 try-catch, 不用为了类型安全写一些冗余代码。
65+
3666
## 用上 `try-flatten`
3767

3868
```ts
39-
const [err, res] = await somePromise;
69+
const [err, res] = await tryFlatten(somePromise);
4070

4171
// 只需要判断 err 是否存在即可
4272
if (err) {
4373
// 此处 err 类型为 Error,res 类型为 undefined
44-
console.log(err instanceof Error);
45-
console.log(res === undefined);
74+
console.log(err instanceof Error); // true
75+
console.log(res === undefined); // true
4676
return;
4777
}
4878

4979
// 此处 err 类型为 null,res 类型为 Result
50-
console.log(err === null);
51-
console.log(res.prop);
80+
console.log(err === null); // true
81+
console.log(res.prop); // 'value'
5282
```
5383

5484
# 下载安装

0 commit comments

Comments
 (0)