no-extra-boolean-cast

禁止不必要的布尔类型转换

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

if 语句的测试等上下文中,表达式的结果已经被强制转换为布尔值,不需要通过双重否定 (!!) 或 Boolean 调用转换为布尔值。例如,这些 if 语句是等效的:

if (!!foo) {
    // ...
}

if (Boolean(foo)) {
    // ...
}

if (foo) {
    // ...
}

规则详情

此规则不允许不必要的布尔类型转换。

zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6dSdz0JClE4rOOJ51S8csCM

/*eslint no-extra-boolean-cast: "error"*/

var foo = !!!bar;

var foo = !!bar ? baz : bat;

var foo = Boolean(!!bar);

var foo = new Boolean(!!bar);

if (!!foo) {
    // ...
}

if (Boolean(foo)) {
    // ...
}

while (!!foo) {
    // ...
}

do {
    // ...
} while (Boolean(foo));

for (; !!foo; ) {
    // ...
}

h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwwaWj0QX+JXnab7YJgxSmDw

/*eslint no-extra-boolean-cast: "error"*/

var foo = !!bar;
var foo = Boolean(bar);

function foo() {
    return !!bar;
}

var foo = bar ? !!baz : !!bat;

选项

ZmISekGbcAWtmV//14mcrLfLIAw6mxSzDDbtsWnEaXF9CZ1nfG32DOC7X6bvDtXM

    00Un8sTxn77+g/0IPQ2/OxMFSRQDgB00H0LAOoQ+BxOHBRKUbc/BDgGMqlGhe0l/26ZeIi+ksMycOLq1tfIyxMZdKnbvFj10KGE8N/GtUJwNFzf/AdRKfdkgllHokylwa9norGCRL+C0a6KiZn211rDhZdiQOJuV0trC8foQm6qomWQTPK5e6MuK/cyEVsrWuWkvjOlXMg2vdSJFdQowxmvtmveQ93AG3SV0qv7B1ql62o3a5gGPgEKkeGCv+N9qSeHtDvdcqp4cK9YJdRAPmveRHAOE080+UmUG3FWc/1mCL6RCasg1csBYeo6tzPi88/bymiQXjECBF3gO+jzmKm0l6jBCLelbLJM+qdMC4Twkt6iveVCEAbwFVpEOqFJ2C/sqSMLgG4b/jsaFCxD9zMKEFwGOR64H/CoENHMgs2Y=

enforceForLogicalOperands

zblpFAyoEiDF1GS7EEgT+Fw+zDBsVLJNOcYSNQAXW6c0/pGXCdZzlgyp/pa/nlUdD91mIbkOyl050XUkB5umMFRMtFooVuh7QsnxNcXkAtswkdDzJOWqBIiBFswrfIuYfesLWSMnbrTN8ViuyrbOyHGbs34lAYkYQO/H8xXv+DQ=

/*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/

if (!!foo || bar) {
    //...
}

while (!!foo && bar) {
    //...
}

if ((!!foo || bar) && baz) {
    //...
}

foo && Boolean(bar) ? baz : bat

var foo = new Boolean(!!bar || baz)

h+1Jf/rEEE6yYiKhWnK/aHl9/UYIfowckm44qhmqvwz4S85n+vKKY8P04wZkXnhXMkgvsJiXG7Sm4SF/WzMTDDfkBeMPqwzo0LJwFGiGBhFcj9sLEDvShMFokRKdeKYxj6mJ+dKEATVnh+H8oFgt/JKEYYygXBhUFhgTKIe/Xm0=

/*eslint no-extra-boolean-cast: ["error", {"enforceForLogicalOperands": true}]*/

if (foo || bar) {
    //...
}

while (foo && bar) {
    //...
}

if ((foo || bar) && baz) {
    //...
}

foo && bar ? baz : bat

var foo = new Boolean(bar || baz)

var foo = !!bar || baz;