docs(types): fix undefined/null #17

This commit is contained in:
ruanyf
2023-08-10 16:39:54 +08:00
parent b0db793204
commit dbe9cb6214

View File

@@ -139,18 +139,18 @@ let c = null; // any
const d = null; // any
```
如果希望避免这种情况,则需要打开编译选项`strictNullChecks`
即使打开编译选项`strictNullChecks``let`命令声明的变量,还是会被推断为`any`,但`const`命令声明的常量会相应被推断为`undefined``null`
```typescript
// 打开编译设置 strictNullChecks
let a = undefined; // undefined
// 打开编译选项 strictNullChecks
let a = undefined; // any
const b = undefined; // undefined
let c = null; // null
let c = null; // any
const d = null; // null
```
上面示例中,打开编译设置`strictNullChecks`以后,赋值为`undefined`的变量会被推断为`undefined`类型,赋值为`null`的变量会被推断为`null`类型
上面示例中,打开编译选项`strictNullChecks`以后,`let`命令的推断结果不变,还是`any`,但是`const`命令的推断结果会更精确,相应为`undefined``null`
## 包装对象类型