TypeScript
bottom type: never
Examples of types that can never happen are:
- functions that never return
- type guards that return false
immutable
immutable 的重要性其实是为了保证 Record,List 等类型运算符的 “元” 不变,也就是基础类型(最小计算单位)的不变:
对于某个一元类型运算符 Foo<?₁>
- 对于任何类型
T
,S
: 如果T
S
,那么Foo<T>
Foo<S>
,称为?₁
关于Foo<?₁>
协变 - 对于任何类型
T
,S
: 如果S
T
,那么Foo<T>
Foo<S>
,称为?₁
关于Foo<?₁>
逆变 - 对于任何类型
T
,S
: 如果 (T
S
而且S
T
),那么Foo<T>
Foo<S>
,称为?₁
关于Foo<?₁>
不变 - 对于任何类型
T
,S
: 如果 (T
S
或者S
T
),那么Foo<T>
Foo<S>
,称为?₁
关于Foo<?₁>
双变
可以看到在判断可变性时,也需要维持 元 不变,所以像之前推导的 Array
当然 Ts 在实现结构类型的同时,也为了照顾 js 的动态可变,没有强制要求 Record 下的 元 不可变(甚至支持了可变下的结构比对),所以如果某个属性确实不存在变化,应该为其设置 readonly,这样的操作通常被用 as const 替代了。
所以正确设置 readonly,为可变的属性不设置,才能正确处理好子类型关系,也就能真正保证安全了。
when safe meets unsafe
不安全其实就是 不可名状,未知(克苏鲁)
Tools
playwright
微软的 e2e 测试
从录制的效果来看优于cypress,如有需要操作后录制测试步骤的,是不错的选择。
https://github.com/microsoft/playwright
Eslint
速度提升
node_modules/.bin/eslint —print-config .eslintrc.js 获取配置信息
Prettier
查看未prettier文件
/node_modules/.bin/prettier --list-different './src/**/*.{tsx,js,json}'
Shell
grep
文本输出筛选,并打印行号:
grep ‘xx’ 匹配
-n 同时左侧显示行号
node_modules/.bin/eslint --print-config .eslintrc.js | grep 'indent' -n
-5n 会展示匹配行的上下 5 行
node_modules/.bin/eslint --print-config .eslintrc.js | grep 'indent' -5n