Appearance
简介
静态类型检
TypeScript 在执行之前,基于值的类型检查程序是否有错误。它是静态类型检查器。当我们访问一个不存在的属性时,TypeScript 会给出错误。
ts
const obj = { width: 10, height: 15 }
const area = obj.width * obj.heigth
// Property 'heigth' does not exist on type '{ width: number; height: number; }'. Did you mean 'height'?
规则增强
TypeScript 内置了一些规则来限制一些操作合理性。例如:数字除以数组 4/[]
在 TypeScript 中被认为是无意义的,并报错。
ts
4 / []
// The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
这些规则是可配置的,可以通过修改 TypeScript 配置文件的方式来控制检查代码的严格程度。