代码点读机 / TypeScript / boolean

boolean TypeScript · 关键字

开关型类型,只有 true/false 两个档位,像墙上的单开电灯开关。

怎么用

let 变量: boolean = true;

小例子

let 已成年: boolean = true;
已成年 = 1;

类型检查结果

不能把类型"number"分配给类型"boolean"

提醒:0/1 不能直接当 boolean 用

官方文档:https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#the-primitives-string-number-and-boolean

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← TypeScript全部词条