TypeScript

bottom type: never

https://thesoftwaresimpleton.com/blog/2019/05/26/ts-bottom-type#:~:text=Bottom%20and%20Top%20types%20in%20typescript%20The%20concept,and%20unknown%20and%20never%20is%20the%20bottom%20type.

Examples of types that can never happen are:

  • functions that never return
  • type guards that return false

immutable

immutable 的重要性其实是为了保证 Record,List 等类型运算符的 “元” 不变,也就是基础类型(最小计算单位)的不变:

对于某个一元类型运算符 Foo<?₁>

  • 对于任何类型 TS: 如果 T21.05.11 - 图1S ,那么 Foo<T>21.05.11 - 图2Foo<S> ,称为 ?₁ 关于 Foo<?₁> 协变
  • 对于任何类型 TS: 如果 S21.05.11 - 图3T ,那么 Foo<T>21.05.11 - 图4Foo<S> ,称为 ?₁ 关于 Foo<?₁> 逆变
  • 对于任何类型 TS: 如果 (T21.05.11 - 图5S 而且 S21.05.11 - 图6T),那么 Foo<T>21.05.11 - 图7Foo<S> ,称为 ?₁ 关于 Foo<?₁> 不变
  • 对于任何类型 TS: 如果 (T21.05.11 - 图8S 或者 S21.05.11 - 图9T),那么 Foo<T>21.05.11 - 图10Foo<S> ,称为 ?₁ 关于 Foo<?₁> 双变

可以看到在判断可变性时,也需要维持 元 不变,所以像之前推导的 Array 支持协变,函数参数支持逆变,也都是建立在 元 不变继而 运算不变的基础之上的。
当然 Ts 在实现结构类型的同时,也为了照顾 js 的动态可变,没有强制要求 Record 下的 元 不可变(甚至支持了可变下的结构比对),所以如果某个属性确实不存在变化,应该为其设置 readonly,这样的操作通常被用 as const 替代了。
所以正确设置 readonly,为可变的属性不设置,才能正确处理好子类型关系,也就能真正保证安全了。

when safe meets unsafe

不安全其实就是 不可名状,未知(克苏鲁)
image.png

Tools

playwright

微软的 e2e 测试
从录制的效果来看优于cypress,如有需要操作后录制测试步骤的,是不错的选择。
https://github.com/microsoft/playwright

Eslint

速度提升
node_modules/.bin/eslint —print-config .eslintrc.js 获取配置信息

Prettier

查看未prettier文件

  1. /node_modules/.bin/prettier --list-different './src/**/*.{tsx,js,json}'

Shell

grep

文本输出筛选,并打印行号:
grep ‘xx’ 匹配
-n 同时左侧显示行号

  1. node_modules/.bin/eslint --print-config .eslintrc.js | grep 'indent' -n

image.png
-5n 会展示匹配行的上下 5 行

  1. node_modules/.bin/eslint --print-config .eslintrc.js | grep 'indent' -5n

image.png