禁止多余的布尔转换
注意:此规则是
recommended 规则集的一部分。在
deno.json 中启用完整集合{
"lint": {
"rules": {
"tags": ["recommended"]
}
}
}使用 Deno CLI 启用完整集合
deno lint --rules-tags=recommended
通过将其添加到
deno.json 中的 include 或 exclude 数组中,可以将此规则明确地包含或排除在当前标签的规则集合中。{
"lint": {
"rules": {
"include": ["no-extra-boolean-cast"],
"exclude": ["no-extra-boolean-cast"]
}
}
}禁止不必要的布尔类型转换。
在某些上下文,例如 if、while 或 for 语句中,表达式会自动强制转换为布尔类型。因此,双重否定(!!foo)或类型转换(Boolean(foo))等技术是不必要的,并且与没有否定或类型转换时产生相同的结果。
无效示例
if (!!foo) {}
if (Boolean(foo)) {}
while (!!foo) {}
for (; Boolean(foo);) {}
有效示例
if (foo) {}
while (foo) {}
for (; foo;) {}