版本

no-implicit-coercion

禁用简写类型转换

🔧 可修复

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

💡 有建议

此规则报告的一些问题可以通过编辑器建议手动修复

❄️ 已冻结

此规则目前处于冻结状态,不接受功能请求。

在 JavaScript 中,有很多不同的方法来转换值类型。 其中一些可能难以阅读和理解。

例如

const b = !!foo;
const b1 = ~foo.indexOf(".");
const n = +foo;
const n1 = -(-foo);
const n2 = foo - 0;
const n3 = 1 * foo;
const s = "" + foo;
foo += ``;

这些可以用以下代码替换

const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;
const n = Number(foo);
const n1 = Number(foo);
const n2 = Number(foo);
const n3 = Number(foo);
const s = String(foo);
foo = String(foo);

规则详情

此规则旨在标记类型转换的简写符号,然后建议更易于理解的符号。

选项

此规则有三个主要选项和一个覆盖选项,以根据需要允许某些强制转换。

  • "boolean"(默认为 true)- 当此项为 true 时,此规则会警告 boolean 类型的简写类型转换。
  • "number"(默认为 true)- 当此项为 true 时,此规则会警告 number 类型的简写类型转换。
  • "string"(默认为 true)- 当此项为 true 时,此规则会警告 string 类型的简写类型转换。
  • "disallowTemplateShorthand"(默认为 false)- 当此项为 true 时,此规则会警告使用 ${expression} 形式的 string 类型转换。
  • "allow"(默认为 empty)- 此数组中的每个条目可以是 ~!!+- --* 中的一个,这些运算符将被允许。

请注意,allow 列表中的运算符 + 将允许 +foo(数字强制转换)以及 "" + foo(字符串强制转换)。

boolean

对于默认的 { "boolean": true } 选项,不正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const b = !!foo;
const b1 = ~foo.indexOf(".");
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.

对于默认的 { "boolean": true } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;

const n = ~foo; // This is a just bitwise not.

number

对于默认的 { "number": true } 选项,不正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const n = +foo;
const n1 = -(-foo);
const n2 = foo - 0;
const n3 = 1 * foo;

对于默认的 { "number": true } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const n = Number(foo);
const n1 = parseFloat(foo);
const n2 = parseInt(foo, 10);

const n3 = foo * 1/4; // `* 1` is allowed when followed by the `/` operator

string

对于默认的 { "string": true } 选项,不正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const s = "" + foo;
const s1 = `` + foo;
foo += "";
foo += ``;

对于默认的 { "string": true } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: "error"*/

const s = String(foo);
foo = String(foo);

disallowTemplateShorthand

此选项不受 string 选项的影响。

对于 { "disallowTemplateShorthand": true } 选项,不正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

const s = `${foo}`;

对于 { "disallowTemplateShorthand": true } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

const s = String(foo);

const s1 = `a${foo}`;

const s2 = `${foo}b`;

const s3 = `${foo}${bar}`;

const s4 = tag`${foo}`;

对于默认的 { "disallowTemplateShorthand": false } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/

const s = `${foo}`;

allow

使用 allow 列表,我们可以覆盖并允许特定的运算符。

对于示例 { "allow": ["!!", "~"] } 选项,正确代码的示例

在 Playground 中打开
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/

const b = !!foo;
const b1 = ~foo.indexOf(".");

何时不使用

如果您不想收到关于类型转换简写符号的通知,您可以安全地禁用此规则。

版本

此规则在 ESLint v1.0.0-rc-2 中引入。

资源

更改语言