Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fab11f31d2 | ||
|
|
8af572d9f9 | ||
|
|
0c542af0ab | ||
|
|
4b3aae675d | ||
|
|
532d3bd6f0 | ||
|
|
cb20d65c2c | ||
|
|
07b6017451 | ||
|
|
2c5f8ca4f3 | ||
|
|
cd543b1fd4 | ||
|
|
88560b6626 |
@@ -6,7 +6,7 @@
|
||||
|
||||
前端界的好文精读,每周更新!
|
||||
|
||||
最新精读:<a href="./前沿技术/207.%E7%B2%BE%E8%AF%BB%E3%80%8ATypescript%20infer%20%E5%85%B3%E9%94%AE%E5%AD%97%E3%80%8B.md">207.精读《Typescript infer 关键字》</a>
|
||||
最新精读:<a href="./前沿技术/210.%E7%B2%BE%E8%AF%BB%E3%80%8Aclass%20static%20block%E3%80%8B.md">210.精读《class static block》</a>
|
||||
|
||||
素材来源:[周刊参考池](https://github.com/ascoders/weekly/issues/2)
|
||||
|
||||
@@ -165,6 +165,9 @@
|
||||
- <a href="./前沿技术/205.%E7%B2%BE%E8%AF%BB%E3%80%8AJS%20with%20%E8%AF%AD%E6%B3%95%E3%80%8B.md">205.精读《JS with 语法》</a>
|
||||
- <a href="./前沿技术/206.%E7%B2%BE%E8%AF%BB%E3%80%8A%E4%B8%80%E7%A7%8D%20Hooks%20%E6%95%B0%E6%8D%AE%E6%B5%81%E7%AE%A1%E7%90%86%E6%96%B9%E6%A1%88%E3%80%8B.md">206.精读《一种 Hooks 数据流管理方案》</a>
|
||||
- <a href="./前沿技术/207.%E7%B2%BE%E8%AF%BB%E3%80%8ATypescript%20infer%20%E5%85%B3%E9%94%AE%E5%AD%97%E3%80%8B.md">207.精读《Typescript infer 关键字》</a>
|
||||
- <a href="./前沿技术/208.%E7%B2%BE%E8%AF%BB%E3%80%8ATypescript%204.4%E3%80%8B.md">208.精读《Typescript 4.4》</a>
|
||||
- <a href="./前沿技术/209.%E7%B2%BE%E8%AF%BB%E3%80%8A%E6%8D%95%E8%8E%B7%E6%89%80%E6%9C%89%E5%BC%82%E6%AD%A5%20error%E3%80%8B.md">209.精读《捕获所有异步 error》</a>
|
||||
- <a href="./前沿技术/210.%E7%B2%BE%E8%AF%BB%E3%80%8Aclass%20static%20block%E3%80%8B.md">210.精读《class static block》</a>
|
||||
|
||||
### 设计模式
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ Typescript 官网也拿 `ReturnType` 这一经典例子说明它的作用:
|
||||
type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
|
||||
```
|
||||
|
||||
理解为:如果 `T` 继承了 `extends (...args: any[]) => any` 类型,则返回类型 `R`,否则返回 `any`。其中 `R` 是什么呢?`R` 被定义在 `extends (...args: any[]) => infer R` 中,即 R 是从传入参数类型中推导出来的。
|
||||
理解为:如果 `T` 继承了 `(...args: any[]) => any` 类型,则返回类型 `R`,否则返回 `any`。其中 `R` 是什么呢?`R` 被定义在 `extends (...args: any[]) => infer R` 中,即 R 是从传入参数类型中推导出来的。
|
||||
|
||||
## 精读
|
||||
|
||||
@@ -36,10 +36,10 @@ function xxx<T>(value: T): { result: T }
|
||||
|
||||
但我们发现 `T` 这个泛型太整体化了,我们还不具备从中 Pick 子类型的能力。也就是对于 `xxx<{label: string}>` 这个场景,`T = {label: string}`,但我们无法将 `R` 定义为 `{label: R}` 这个位置,因为泛型是一个不可拆分的整体。
|
||||
|
||||
而且实际上为了类型安全,我们也不能允许用户描述任意的类型位置,**万一传入的类型结构不是 `{label: xxx}` 而是一个回调 `() => void`,那子类型推导岂不是建立在了错误的环境中。** 所以考虑到想要拿到 `{label: infer R}`,首先参数必须具备 `{label: xxx}` 的结构,所以正好可以将 `infer` 与条件判断 `T extends ? A : B` 结合起来用,即:
|
||||
而且实际上为了类型安全,我们也不能允许用户描述任意的类型位置,**万一传入的类型结构不是 `{label: xxx}` 而是一个回调 `() => void`,那子类型推导岂不是建立在了错误的环境中。** 所以考虑到想要拿到 `{label: infer R}`,首先参数必须具备 `{label: xxx}` 的结构,所以正好可以将 `infer` 与条件判断 `T extends xxx ? A : B` 结合起来用,即:
|
||||
|
||||
```typescript
|
||||
type GetLabelTypeFromObject<T> = T extends ? { label: infer R } ? R : never
|
||||
type GetLabelTypeFromObject<T> = T extends { label: infer R } ? R : never
|
||||
|
||||
type Result = GetLabelTypeFromObject<{ label: string }>;
|
||||
// type Result = string
|
||||
@@ -50,7 +50,7 @@ type Result = GetLabelTypeFromObject<{ label: string }>;
|
||||
回过头来看第一个需求,拿到第一个参数类型就可以用 `infer` 实现了:
|
||||
|
||||
```typescript
|
||||
type GetFirstParamType<T> = T extends ? (...args: infer R) => any ? R[0] : never
|
||||
type GetFirstParamType<T> = T extends (...args: infer R) => any ? R[0] : never
|
||||
```
|
||||
|
||||
可以理解为,如果此时 `T` 满足 `(...args: any) => any` 这个结构,同时我们用 `infer R` 表示 `R` 这个临时变量指代第一个 `any` 运行时类型,那么整个函数返回的类型就是 `R`。如果 `T` 都不满足 `(...args: any) => any` 这个结构,比如 `GetFirstParamType<number>`,那这种推导根本无从谈起,直接返回 `never` 类型兜底,当然也可以自定义比如 `any` 之类的任何类型。
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
Typescript 4.4 正式发布了!距离 Typescript 4.5 发布还有三个月的时间,抓紧上车学习吧!
|
||||
|
||||
本周精读的文章:[announcing-typescript-4-4](https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/)
|
||||
|
||||
## 概述
|
||||
|
||||
### 更智能的自动类型收窄
|
||||
|
||||
类型收窄功能非常方便,它可以让 Typescript 尽可能的像 Js 一样自动智能判定类型,从而避免类型定义的工作,让你的 Typescript 写得更像 Js。
|
||||
|
||||
其实这个功能早就有了,在我们 [精读《Typescript2.0 - 2.9》](https://github.com/ascoders/weekly/blob/master/%E5%89%8D%E6%B2%BF%E6%8A%80%E6%9C%AF/58.%E7%B2%BE%E8%AF%BB%E3%80%8ATypescript2.0%20-%202.9%E3%80%8B.md#%E8%87%AA%E5%8A%A8%E7%B1%BB%E5%9E%8B%E6%8E%A8%E5%AF%BC) 就已经介绍过,当时用的名词是自动类型推导,这次用了更精确的自动类型收窄一词,因为只有类型收窄是安全的,比如:
|
||||
|
||||
```typescript
|
||||
function foo(arg: unknown) {
|
||||
if (typeof arg === "string") {
|
||||
// We know 'arg' is a string now.
|
||||
console.log(arg.toUpperCase());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
而在 Typescript 4.4 之前的版本,如果我们将这个判定赋值给一个变量,再用到 `if` 分支里,就无法正常收窄类型了:
|
||||
|
||||
```typescript
|
||||
function foo(arg: unknown) {
|
||||
const argIsString = typeof arg === "string";
|
||||
if (argIsString) {
|
||||
console.log(arg.toUpperCase());
|
||||
// ~~~~~~~~~~~
|
||||
// Error! Property 'toUpperCase' does not exist on type 'unknown'.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
这个问题在 Typescript 4.4 得到了解决,实际上是把这种类型收窄判断逻辑加深了,即无论这个判断写在哪都可以生效。所以下面这种解构的用法判断也可以推断出类型收窄:
|
||||
|
||||
```typescript
|
||||
type Shape =
|
||||
| { kind: "circle", radius: number }
|
||||
| { kind: "square", sideLength: number };
|
||||
|
||||
function area(shape: Shape): number {
|
||||
// Extract out the 'kind' field first.
|
||||
const { kind } = shape;
|
||||
|
||||
if (kind === "circle") {
|
||||
// We know we have a circle here!
|
||||
return Math.PI * shape.radius ** 2;
|
||||
}
|
||||
else {
|
||||
// We know we're left with a square here!
|
||||
return shape.sideLength ** 2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
不仅是单一的判断,Typescript 4.4 还支持复合类型推导:
|
||||
|
||||
```typescript
|
||||
function doSomeChecks(
|
||||
inputA: string | undefined,
|
||||
inputB: string | undefined,
|
||||
shouldDoExtraWork: boolean,
|
||||
) {
|
||||
const mustDoWork = inputA && inputB && shouldDoExtraWork;
|
||||
if (mustDoWork) {
|
||||
// We can access 'string' properties on both 'inputA' and 'inputB'!
|
||||
const upperA = inputA.toUpperCase();
|
||||
const upperB = inputB.toUpperCase();
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
`mustDoWork` 为 `true` 的分支就意味着 `inputA`、`inputB` 均收窄为 `string` 类型。
|
||||
|
||||
这种深层的判定还体现在,一个具备类型判断的变量进行再计算,生成的变量还具有类型判断功能:
|
||||
|
||||
```typescript
|
||||
function f(x: string | number | boolean) {
|
||||
const isString = typeof x === "string";
|
||||
const isNumber = typeof x === "number";
|
||||
const isStringOrNumber = isString || isNumber;
|
||||
if (isStringOrNumber) {
|
||||
x; // Type of 'x' is 'string | number'.
|
||||
}
|
||||
else {
|
||||
x; // Type of 'x' is 'boolean'.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
可以看到,我们几乎可以像写 Js 一样写 Typescript,4.4 支持了大部分符合直觉的推导非常方便。但要注意的是,Typescript
|
||||
毕竟不是运行时,无法做到更彻底的自动推断,但足以支持绝大部分场景。
|
||||
|
||||
### 下标支持 Symbol 与模版字符串类型判定
|
||||
|
||||
原本我们定义一个用下标访问的对象是这样的:
|
||||
|
||||
```typescript
|
||||
interface Values {
|
||||
[key: string]: number
|
||||
}
|
||||
```
|
||||
|
||||
现在也支持 Symbol 拉:
|
||||
|
||||
```typescript
|
||||
interface Colors {
|
||||
[sym: symbol]: number;
|
||||
}
|
||||
|
||||
const red = Symbol("red");
|
||||
const green = Symbol("green");
|
||||
const blue = Symbol("blue");
|
||||
|
||||
let colors: Colors = {};
|
||||
|
||||
colors[red] = 255; // Assignment of a number is allowed
|
||||
let redVal = colors[red]; // 'redVal' has the type 'number'
|
||||
|
||||
colors[blue] = "da ba dee"; // Error: Type 'string' is not assignable to type 'number'.
|
||||
```
|
||||
|
||||
而且对于特定的字符串模版也支持类型匹配,比如希望以 `data-` 开头的下标是一种独立类型,可以这么定义:
|
||||
|
||||
```typescript
|
||||
interface Options {
|
||||
width?: number;
|
||||
height?: number;
|
||||
}
|
||||
|
||||
let a: Options = {
|
||||
width: 100,
|
||||
height: 100,
|
||||
"data-blah": true, // Error! 'data-blah' wasn't declared in 'Options'.
|
||||
};
|
||||
|
||||
interface OptionsWithDataProps extends Options {
|
||||
// Permit any property starting with 'data-'.
|
||||
[optName: `data-${string}`]: unknown;
|
||||
}
|
||||
|
||||
let b: OptionsWithDataProps = {
|
||||
width: 100,
|
||||
height: 100,
|
||||
"data-blah": true, // Works!
|
||||
|
||||
"unknown-property": true, // Error! 'unknown-property' wasn't declared in 'OptionsWithDataProps'.
|
||||
};
|
||||
```
|
||||
|
||||
这个对于 HTML 的 `data-` 属性非常有帮助。
|
||||
|
||||
同时还支持联合类型定义,下面两种类型定义方式是等价的:
|
||||
|
||||
```typescript
|
||||
interface Data {
|
||||
[optName: string | symbol]: any;
|
||||
}
|
||||
|
||||
// Equivalent to
|
||||
|
||||
interface Data {
|
||||
[optName: string]: any;
|
||||
[optName: symbol]: any;
|
||||
}
|
||||
```
|
||||
|
||||
### 更严格的错误捕获类型
|
||||
|
||||
在 `unknown` 类型出来之前,Typescript 以 `any` 作为抛出错误的默认类型,毕竟谁也不知道抛出错误的类型是什么:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
// Who knows what this might throw...
|
||||
executeSomeThirdPartyCode();
|
||||
}
|
||||
catch (err) { // err: any
|
||||
console.error(err.message); // Allowed, because 'any'
|
||||
err.thisWillProbablyFail(); // Allowed, because 'any' :(
|
||||
}
|
||||
```
|
||||
|
||||
Who knows what this might throw... 这句话很有意思,一个函数任何地方都可能出现运行时错误,这根本不是静态分析可以解决的,所以不可能自动推断错误类型,所以只能用 `any`。
|
||||
|
||||
在 Typescript 4.4 的 `--useUnknownInCatchVariables` 或 `--strict` 模式下都将以 `unknown` 作为捕获到错误的默认类型。
|
||||
|
||||
相比不存在的类型 `never`,`unknown` 仅仅是不知道是什么类型而已,所以不能像 `any` 一样当作任何类型使用,但我们可以将其随意推断为任意类型:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
executeSomeThirdPartyCode();
|
||||
}
|
||||
catch (err) { // err: unknown
|
||||
// Error! Property 'message' does not exist on type 'unknown'.
|
||||
console.error(err.message);
|
||||
|
||||
// Works! We can narrow 'err' from 'unknown' to 'Error'.
|
||||
if (err instanceof Error) {
|
||||
console.error(err.message);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
如果觉得这样做麻烦,也可以重新申明类型为 `any`:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
executeSomeThirdPartyCode();
|
||||
}
|
||||
catch (err: any) {
|
||||
console.error(err.message); // Works again!
|
||||
}
|
||||
```
|
||||
|
||||
但这样做其实并不合适,因为即便是考虑了运行时因素,理论上还是可能发生意外错误,所以对错误过于自信的类型推断是不太合适的,最好保持其 `unknown` 类型,对所有可能的边界情况做处理。
|
||||
|
||||
### 明确的可选属性
|
||||
|
||||
对象的可选属性在类型描述时有个含糊不清的地方,比如:
|
||||
|
||||
```typescript
|
||||
interface Person {
|
||||
name: string,
|
||||
age?: number;
|
||||
}
|
||||
```
|
||||
|
||||
其实 Typescript 对其的类型定义的是:
|
||||
|
||||
```typescript
|
||||
interface Person {
|
||||
name: string,
|
||||
age?: number | undefined;
|
||||
}
|
||||
```
|
||||
|
||||
为什么要这么定义呢?因为很多情况下,没有这个 key,与这个 key 的值为 `undefined` 的表现是等价的。但比如 `Object.keys` 场景下这两种表现却又不等价,所以理论上对于 `age?: number` 的确切表述是:要么没有 `age`,要么有 `age` 且类型为 `number`,也就是说下面的写法应该是错误的:
|
||||
|
||||
```typescript
|
||||
// With 'exactOptionalPropertyTypes' on:
|
||||
const p: Person = {
|
||||
name: "Daniel",
|
||||
age: undefined, // Error! undefined isn't a number
|
||||
};
|
||||
```
|
||||
|
||||
在 Typescript 4.4 中同时开启 `--exactOptionalPropertyTypes` 与 `--strictNullChecks` 即可生效。
|
||||
|
||||
仔细想想这是合理的,既然定义的类型不是 `undefined`,就算对象是可选类型,也不能认为赋值 `undefined` 是合理的,因为 `age?: number` 的心理预期是,要么没有这个 key,要么有但是类型为 `number`,所以当 `Object.keys` 发现 `age` 这个 key 时,值就应该是 `number`。
|
||||
|
||||
### 支持 Static Block
|
||||
|
||||
Typescript 4.4 支持了 [class static blocks](https://github.com/tc39/proposal-class-static-block#ecmascript-class-static-initialization-blocks),并且在代码块作用域内可以访问私有变量。
|
||||
|
||||
|
||||
还有一些性能提升与体验优化杂项就不一一列举了,感兴趣可以直接看原文档:[perf-improvements](https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#perf-improvements)。
|
||||
|
||||
|
||||
## 总结
|
||||
|
||||
从 Typescript 4.4 特性可以看出,Typescript 正在往 “更具备原生 JS 亲和性” 方向作出努力,这无疑会使 Typescript 变得越来越好用。
|
||||
|
||||
对更多新特性感兴趣,可以 [查看 Typescript 4.5 版本发布计划](https://github.com/microsoft/TypeScript/issues/45418)。
|
||||
|
||||
> 讨论地址是:[精读《Typescript 4.4》· Issue #348 · dt-fe/weekly](https://github.com/dt-fe/weekly/issues/348)
|
||||
|
||||
**如果你想参与讨论,请 [点击这里](https://github.com/dt-fe/weekly),每周都有新的主题,周末或周一发布。前端精读 - 帮你筛选靠谱的内容。**
|
||||
|
||||
> 关注 **前端精读微信公众号**
|
||||
|
||||
<img width=200 src="https://img.alicdn.com/tfs/TB165W0MCzqK1RjSZFLXXcn2XXa-258-258.jpg">
|
||||
|
||||
> 版权声明:自由转载-非商用-非衍生-保持署名([创意共享 3.0 许可证](https://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh))
|
||||
@@ -0,0 +1,255 @@
|
||||
成熟的产品都有较高的稳定性要求,仅前端就要做大量监控、错误上报,后端更是如此,一个未考虑的异常可能导致数据错误、服务雪崩、内存溢出等等问题,轻则每天焦头烂额的处理异常,重则引发线上故障。
|
||||
|
||||
假设代码逻辑没有错误,那么剩下的就是异常错误了。
|
||||
|
||||
由于任何服务、代码都可能存在外部调用,只要外部调用存在不确定性,代码就可能出现异常,所以捕获异常是一个非常重要的基本功。
|
||||
|
||||
所以本周就精读 [How to avoid uncaught async errors in Javascript](https://advancedweb.hu/how-to-avoid-uncaught-async-errors-in-javascript/) 这篇文章,看看 JS 如何捕获异步异常错误。
|
||||
|
||||
## 概述
|
||||
|
||||
之所以要关注异步异常,是因为捕获同步异常非常简单:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
;(() => {
|
||||
throw new Error('err')
|
||||
})()
|
||||
} catch (e) {
|
||||
console.log(e) // caught
|
||||
}
|
||||
```
|
||||
|
||||
但异步错误却无法被直接捕获,这不太直观:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
;(async () => {
|
||||
throw new Error('err') // uncaught
|
||||
})()
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
```
|
||||
|
||||
原因是异步代码并不在 `try catch` 上下文中执行,唯一的同步逻辑只有创建一个异步函数,所以异步函数内的错误无法被捕获。
|
||||
|
||||
要捕获 `async` 函数内的异常,可以调用 `.catch`,因为 `async` 函数返回一个 Promise:
|
||||
|
||||
```typescript
|
||||
;(async () => {
|
||||
throw new Error('err')
|
||||
})().catch((e) => {
|
||||
console.log(e) // caught
|
||||
})
|
||||
```
|
||||
|
||||
当然也可以在函数体内直接用 `try catch`:
|
||||
|
||||
```typescript
|
||||
;(async () => {
|
||||
try {
|
||||
throw new Error('err')
|
||||
} catch (e) {
|
||||
console.log(e) // caught
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
类似的,如果在循环体里捕获异常,则要使用 `Promise.all`:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
await Promise.all(
|
||||
[1, 2, 3].map(async () => {
|
||||
throw new Error('err')
|
||||
})
|
||||
)
|
||||
} catch (e) {
|
||||
console.log(e) // caught
|
||||
}
|
||||
```
|
||||
|
||||
也就是说 `await` 修饰的 Promise 内抛出的异常,可以被 `try catch` 捕获。
|
||||
|
||||
但不是说写了 `await` 就一定能捕获到异常,一种情况是 Promise 内再包含一个异步:
|
||||
|
||||
```typescript
|
||||
new Promise(() => {
|
||||
setTimeout(() => {
|
||||
throw new Error('err') // uncaught
|
||||
}, 0)
|
||||
}).catch((e) => {
|
||||
console.log(e)
|
||||
})
|
||||
```
|
||||
|
||||
这个情况要用 `reject` 方式抛出异常才能被捕获:
|
||||
|
||||
```typescript
|
||||
new Promise((res, rej) => {
|
||||
setTimeout(() => {
|
||||
rej('err') // caught
|
||||
}, 0)
|
||||
}).catch((e) => {
|
||||
console.log(e)
|
||||
})
|
||||
```
|
||||
|
||||
另一种情况是,这个 `await` 没有被执行到:
|
||||
|
||||
```typescript
|
||||
const wait = (ms) => new Promise((res) => setTimeout(res, ms))
|
||||
|
||||
;(async () => {
|
||||
try {
|
||||
const p1 = wait(3000).then(() => {
|
||||
throw new Error('err')
|
||||
}) // uncaught
|
||||
await wait(2000).then(() => {
|
||||
throw new Error('err2')
|
||||
}) // caught
|
||||
await p1
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
`p1` 等待 3s 后抛出异常,但因为 2s 后抛出了 `err2` 异常,中断了代码执行,所以 `await p1` 不会被执行到,导致这个异常不会被 catch 住。
|
||||
|
||||
而且有意思的是,如果换一个场景,提前执行了 `p1`,等 1s 后再 `await p1`,那异常就从无法捕获变成可以捕获了,这样浏览器会怎么处理?
|
||||
|
||||
```typescript
|
||||
const wait = (ms) => new Promise((res) => setTimeout(res, ms))
|
||||
|
||||
;(async () => {
|
||||
try {
|
||||
const p1 = wait(1000).then(() => {
|
||||
throw new Error('err')
|
||||
})
|
||||
await wait(2000)
|
||||
await p1
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
})()
|
||||
```
|
||||
|
||||
结论是浏览器 1s 后会抛出一个未捕获异常,但再过 1s 这个未捕获异常就消失了,变成了捕获的异常。
|
||||
|
||||
这个行为很奇怪,当程序复杂时很难排查,因为并行的 Promise 建议用 Promise.all 处理:
|
||||
|
||||
```typescript
|
||||
await Promise.all([
|
||||
wait(1000).then(() => {
|
||||
throw new Error('err')
|
||||
}), // p1
|
||||
wait(2000),
|
||||
])
|
||||
```
|
||||
|
||||
另外 Promise 的错误会随着 Promise 链传递,因此建议把 Promise 内多次异步行为改写为多条链的模式,在最后 `catch` 住错误。
|
||||
|
||||
还是之前的例子,Promise 无法捕获内部的异步错误:
|
||||
|
||||
```typescript
|
||||
new Promise((res, rej) => {
|
||||
setTimeout(() => {
|
||||
throw Error('err')
|
||||
}, 1000) // 1
|
||||
}).catch((error) => {
|
||||
console.log(error)
|
||||
})
|
||||
```
|
||||
|
||||
但如果写成 Promise Chain,就可以捕获了:
|
||||
|
||||
```typescript
|
||||
new Promise((res, rej) => {
|
||||
setTimeout(res, 1000) // 1
|
||||
})
|
||||
.then((res, rej) => {
|
||||
throw Error('err')
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error)
|
||||
})
|
||||
```
|
||||
|
||||
原因是,用 Promise Chain 代替了内部多次异步嵌套,这样多个异步行为会被拆解为对应 Promise Chain 的同步行为,Promise 就可以捕获啦。
|
||||
|
||||
最后,DOM 事件监听内抛出的错误都无法被捕获:
|
||||
|
||||
```typescript
|
||||
document.querySelector('button').addEventListener('click', async () => {
|
||||
throw new Error('err') // uncaught
|
||||
})
|
||||
```
|
||||
|
||||
同步也一样:
|
||||
|
||||
```typescript
|
||||
document.querySelector('button').addEventListener('click', () => {
|
||||
throw new Error('err') // uncaught
|
||||
})
|
||||
```
|
||||
|
||||
只能通过函数体内 `try catch` 来捕获。
|
||||
|
||||
## 精读
|
||||
|
||||
我们开篇提到了要监控所有异常,仅通过 `try catch`、`then` 捕获同步、异步错误还是不够的,因为这些是局部错误捕获手段,当我们无法保证所有代码都处理了异常时,需要进行全局异常监控,一般有两种方法:
|
||||
|
||||
- `window.addEventListener('error')`
|
||||
- `window.addEventListener('unhandledrejection')`
|
||||
|
||||
`error` 可以监听所有同步、异步的运行时错误,但无法监听语法、接口、资源加载错误。而 `unhandledrejection` 可以监听到 Promise 中抛出的,未被 `.catch` 捕获的错误。
|
||||
|
||||
在具体的前端框架中,也可以通过框架提供的错误监听方案解决部分问题,比如 React 的 [Error Boundaries](https://reactjs.org/docs/error-boundaries.html)、Vue 的 [error handler](https://v3.vuejs.org/api/application-config.html#errorhandler),一个是 UI 组件级别的,一个是全局的。
|
||||
|
||||
回过头来看,本身 js 提供的 `try catch` 错误捕获是非常有效的,之所以会遇到无法捕获错误的经常,大多是因为异步导致的。
|
||||
|
||||
然而大部分异步错误,都可以通过 `await` 的方式解决,我们唯一要注意的是,`await` 仅支持一层,或者说一条链的错误监听,比如这个例子是可以监听到错误的:
|
||||
|
||||
```typescript
|
||||
try {
|
||||
await func1()
|
||||
} catch (err) {
|
||||
// caught
|
||||
}
|
||||
|
||||
async function func1() {
|
||||
await func2()
|
||||
}
|
||||
|
||||
async function func2() {
|
||||
throw Error('error')
|
||||
}
|
||||
```
|
||||
|
||||
也就是说,只要这一条链内都被 `await` 住了,那么最外层的 `try catch` 就能捕获异步错误。但如果有一层异步又脱离了 `await`,那么就无法捕获了:
|
||||
|
||||
```typescript
|
||||
async function func2() {
|
||||
setTimeout(() => {
|
||||
throw Error('error') // uncaught
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
针对这个问题,原文也提供了例如 `Promise.all`、链式 Promise、`.catch` 等方法解决,因此只要编写代码时注意对异步的处理,就可以用 `try catch` 捕获这些异步错误。
|
||||
|
||||
## 总结
|
||||
|
||||
关于异步错误的处理,如果还有其它未考虑到的情况,欢迎留言补充。
|
||||
|
||||
> 讨论地址是:[精读《捕获所有异步 error》· Issue #350 · dt-fe/weekly](https://github.com/dt-fe/weekly/issues/350)
|
||||
|
||||
**如果你想参与讨论,请 [点击这里](https://github.com/dt-fe/weekly),每周都有新的主题,周末或周一发布。前端精读 - 帮你筛选靠谱的内容。**
|
||||
|
||||
> 关注 **前端精读微信公众号**
|
||||
|
||||
<img width=200 src="https://img.alicdn.com/tfs/TB165W0MCzqK1RjSZFLXXcn2XXa-258-258.jpg">
|
||||
|
||||
> 版权声明:自由转载-非商用-非衍生-保持署名([创意共享 3.0 许可证](https://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh))
|
||||
@@ -0,0 +1,142 @@
|
||||
[class-static-block](https://github.com/tc39/proposal-class-static-block) 提案于 [2021.9.1](https://github.com/tc39/proposal-class-static-block/commit/c0cabee0aa2d036a8d902fea7bc1d179e3de2477) 进入 stage4,是一个基于 Class 增强的提案。
|
||||
|
||||
本周我们结合 [ES2022 feature: class static initialization blocks](https://2ality.com/2021/09/class-static-block.html) 这篇文章一起讨论一下这个特性。
|
||||
|
||||
## 概述
|
||||
|
||||
为什么我们需要 class static block 这个语法呢?其中一个原因是对 Class 静态变量的灵活赋值需求。以下面为例,我们想在 Class 内部对静态变量做批量初始化,就不得不写一个无用的 `_` 变量用来做初始化的逻辑:
|
||||
|
||||
```typescript
|
||||
class Translator {
|
||||
static translations = {
|
||||
yes: 'ja',
|
||||
no: 'nein',
|
||||
maybe: 'vielleicht',
|
||||
};
|
||||
static englishWords = [];
|
||||
static germanWords = [];
|
||||
static _ = initializeTranslator( // (A)
|
||||
this.translations, this.englishWords, this.germanWords);
|
||||
}
|
||||
function initializeTranslator(translations, englishWords, germanWords) {
|
||||
for (const [english, german] of Object.entries(translations)) {
|
||||
englishWords.push(english);
|
||||
germanWords.push(german);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
而且我们为什么把 `initializeTranslator` 写在外面呢?就因为在 Class 内部不能写代码块,但这造成一个严重的问题,是外部函数无法访问 Class 内部属性,所以需要做一堆枯燥的传值。
|
||||
|
||||
从这个例子看出,我们为了自定义一段静态变量初始化逻辑,需要做出两个妥协:
|
||||
|
||||
1. 在外部定义一个函数,并接受大量 Class 成员变量传参。
|
||||
2. 在 Class 内部定义一个无意义的变量 `_` 用来启动这个函数逻辑。
|
||||
|
||||
这实在太没有代码追求了,我们在 Class 内部做掉这些逻辑不就简洁了吗?这就是 class static block 特性:
|
||||
|
||||
```typescript
|
||||
class Translator {
|
||||
static translations = {
|
||||
yes: 'ja',
|
||||
no: 'nein',
|
||||
maybe: 'vielleicht',
|
||||
};
|
||||
static englishWords = [];
|
||||
static germanWords = [];
|
||||
static { // (A)
|
||||
for (const [english, german] of Object.entries(this.translations)) {
|
||||
this.englishWords.push(english);
|
||||
this.germanWords.push(german);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
可以看到,`static` 关键字后面不跟变量,而是直接跟一个代码块,就是 class static block 语法的特征,在这个代码块内部,可以通过 `this` 访问 Class 所有成员变量,包括 `#` 私有变量。
|
||||
|
||||
原文对这个特性使用介绍就结束了,最后还提到一个细节,就是执行顺序。即所有 `static` 变量或区块都按顺序执行,父类优先执行:
|
||||
|
||||
```typescript
|
||||
class SuperClass {
|
||||
static superField1 = console.log('superField1');
|
||||
static {
|
||||
assert.equal(this, SuperClass);
|
||||
console.log('static block 1 SuperClass');
|
||||
}
|
||||
static superField2 = console.log('superField2');
|
||||
static {
|
||||
console.log('static block 2 SuperClass');
|
||||
}
|
||||
}
|
||||
|
||||
class SubClass extends SuperClass {
|
||||
static subField1 = console.log('subField1');
|
||||
static {
|
||||
assert.equal(this, SubClass);
|
||||
console.log('static block 1 SubClass');
|
||||
}
|
||||
static subField2 = console.log('subField2');
|
||||
static {
|
||||
console.log('static block 2 SubClass');
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
// 'superField1'
|
||||
// 'static block 1 SuperClass'
|
||||
// 'superField2'
|
||||
// 'static block 2 SuperClass'
|
||||
// 'subField1'
|
||||
// 'static block 1 SubClass'
|
||||
// 'subField2'
|
||||
// 'static block 2 SubClass'
|
||||
```
|
||||
|
||||
所以 Class 内允许有多个 class static block,父类和子类也可以有,不同执行顺序结果肯定不同,这个选择权交给了使用者,因为执行顺序和书写顺序一致。
|
||||
|
||||
## 精读
|
||||
|
||||
结合提案来看,class static block 还有一个动机,就是给了一个访问私有变量的机制:
|
||||
|
||||
```typescript
|
||||
let getX;
|
||||
|
||||
export class C {
|
||||
#x
|
||||
constructor(x) {
|
||||
this.#x = { data: x };
|
||||
}
|
||||
|
||||
static {
|
||||
// getX has privileged access to #x
|
||||
getX = (obj) => obj.#x;
|
||||
}
|
||||
}
|
||||
|
||||
export function readXData(obj) {
|
||||
return getX(obj).data;
|
||||
}
|
||||
```
|
||||
|
||||
理论上外部无论如何都无法访问 Class 私有变量,但上面例子的 `readXData` 就可以,而且不会运行时报错,原因就是其整个流程都是合法的,最重要的原因是,class static block 可以同时访问私有变量与全局变量,所以可以利用其做一个 “里应外合”。
|
||||
|
||||
不过我并不觉得这是一个好点子,反而像一个 "BUG",因为任何对规定的突破都会为可维护性埋下隐患,除非这个特性用在稳定的工具、框架层,用来做一些便利性工作,最终提升了应用编码的体验,这种用法是可以接受的。
|
||||
|
||||
最后要意识到,class static block 本质上并没有增加新功能,我们完全可以用普通静态变量代替,只是写起来很不自然,所以这个特性可以理解为对缺陷的补充,或者是语法完善。
|
||||
|
||||
## 总结
|
||||
|
||||
总的来说,class static block 在 Class 内创建了一个块状作用域,这个作用域内拥有访问 Class 内部私有变量的特权,且这个块状作用域仅在引擎调用时初始化执行一次,是一个比较方便的语法。
|
||||
|
||||
原文下方有一些反对声音,说这是对 JS 的复杂化,也有诸如 JS 越来越像 Java 的声音,不过我更赞同作者的观点,也就是 Js 中 Class 并不是全部,现在越来越多代码使用函数式语法,即便使用了 Class 的场景也会存在大量函数申明,所以 class static block 这个提案对开发者的感知实际上并不大。
|
||||
|
||||
> 讨论地址是:[精读《class static block》· Issue #351 · dt-fe/weekly](https://github.com/dt-fe/weekly/issues/351)
|
||||
|
||||
**如果你想参与讨论,请 [点击这里](https://github.com/dt-fe/weekly),每周都有新的主题,周末或周一发布。前端精读 - 帮你筛选靠谱的内容。**
|
||||
|
||||
> 关注 **前端精读微信公众号**
|
||||
|
||||
<img width=200 src="https://img.alicdn.com/tfs/TB165W0MCzqK1RjSZFLXXcn2XXa-258-258.jpg">
|
||||
|
||||
> 版权声明:自由转载-非商用-非衍生-保持署名([创意共享 3.0 许可证](https://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh))
|
||||
@@ -78,7 +78,7 @@ ES 模块需要借助模块加载器来实现这三步。加载器在不同的
|
||||
|
||||
这就意味着我们必须一层一层的遍历文件树,转化文件并找出依赖,最后查找并且加载这些依赖。如果主线程正在等待去下载这些文件,那么很多的任务会堆积在队列中。这是因为浏览器环境下下载用了很长时间。
|
||||
|
||||
阻塞主线程会导致应用所需的模块变得很慢。将构建过程分片进行实现了在全部下载前进行获取和构建。这种查分构建的方式是 ES 模块和 CJS 模块最本质的不同。
|
||||
阻塞主线程会导致应用所需的模块变得很慢。将构建过程分片进行实现了在全部下载前进行获取和构建。这种差分构建的方式是 ES 模块和 CJS 模块最本质的不同。
|
||||
|
||||

|
||||
|
||||
|
||||
Reference in New Issue
Block a user