不能将类型“Boolean”分配给类型“boolean | undefined”
时间: 2023-11-09 11:04:40
浏览: 641
这个错误是 Type[script](https://wenku.csdn.net/doc/64abae142d07955edb5e5066?spm=1055.2569.3001.10083) 提供的类型检查错误,意思是你试图将一个布尔类型(Boolean)赋值给一个可能是布尔类型,也可能是 un[defined](https://wenku.csdn.net/doc/6453207eea0840391e76ea96?spm=1055.2569.3001.10083) 的变量。例如:
```[typescript](https://wenku.csdn.net/doc/4q1oeragxk?spm=1055.2569.3001.10083)
let myVar: boolean | [undefined](https://wenku.csdn.net/doc/6412b4a9be7fbd1778d405dd?spm=1055.2569.3001.10083);
myVar = true; // 没问题
myVar = false; // 没问题
myVar = undefined; // 没问题
myVar = Boolean(true); // 没问题
myVar = Boolean(false); // 没问题
myVar = Boolean(undefined); // 没问题
myVar = Boolean(null); // 没问题
myVar = Boolean('[hello](https://wenku.csdn.net/doc/7xwt3uk76a?spm=1055.2569.3001.10083)'); // 没问题
myVar = Boolean(0); // 报错:不能将类型“Boolean”分配给类型“boolean | undefined”
```
最低
0.47元/天
开通会员,查看完整答案

成为会员后, 你将解锁


相关推荐















