Skip to content

noImplicitCoercions

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"complexity": {
"noImplicitCoercions": "error"
}
}
}
}

¥Description

禁止使用简写类型转换。

¥Disallow shorthand type conversions.

JavaScript 允许使用 !!+~ 等运算符进行简写类型转换。这些简写可能会使代码更难阅读和理解,尤其对于不熟悉这些模式的开发者而言。使用显式类型转换函数(例如 Boolean()Number()String())可以使代码意图更清晰、更易读。

¥JavaScript allows shorthand type conversions by using operators like !!, +, ~, etc. These shortcuts can make the code harder to read and understand, especially for developers who are not familiar with these patterns. Using explicit type conversion functions like Boolean(), Number(), and String() makes the intent clearer and more readable.

此规则会在值转换为以下类型时发出警告:

¥This rule reports when values are converted to:

  • 使用双重否定的布尔值 !!value

    ¥Boolean using double negation !!value

  • 使用一元加号 +value、从零减号 value - 0、乘以 1 value * 1、除以 1 value / 1 或使用减号 -(-value) 进行双重取反。

    ¥Number using unary plus +value, subtraction from zero value - 0, multiplication by one value * 1, division by one value / 1, or double negation with minus -(-value)

  • 使用空字符串 value + "" 或空模板字面量 value + 进行字符串拼接。

    ¥String using concatenation with empty string value + "" or empty template literal value +

  • 使用 indexOf ~value.indexOf(item)“ 进行按位非运算来检查索引,而不是与 -1 比较。

    ¥Check index using bitwise NOT with indexOf ~value.indexOf(item) instead of comparing with -1

¥Examples

¥Invalid

!!foo;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ !!foo;
^^^^^
2 │

Unsafe fix: Use Boolean() call instead.

1 - !!foo;
1+ Boolean(foo);
2 2

+foo;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ +foo;
^^^^
2 │

Unsafe fix: Use Number() call instead.

1 - +foo;
1+ Number(foo);
2 2

-(-foo);
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ -(-foo);
^^^^^^^
2 │

Unsafe fix: Use Number() call instead.

1 - -(-foo);
1+ Number(foo);
2 2

foo - 0;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ foo - 0;
^^^^^^^
2 │

Unsafe fix: Use Number() call instead.

1 - foo·-·0;
1+ Number(foo);
2 2

foo * 1;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ foo * 1;
^^^^^^^
2 │

Unsafe fix: Use Number() call instead.

1 - foo·*·1;
1+ Number(foo);
2 2

foo / 1;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ foo / 1;
^^^^^^^
2 │

Unsafe fix: Use Number() call instead.

1 - foo·/·1;
1+ Number(foo);
2 2

"" + foo;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ "" + foo;
^^^^^^^^
2 │

Unsafe fix: Use String() call instead.

1 - ""·+·foo;
1+ String(foo);
2 2

foo + "";
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ foo + "";
^^^^^^^^
2 │

Unsafe fix: Use String() call instead.

1 - foo·+·"";
1+ String(foo);
2 2

`` + foo;
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ “ + foo;
^^^^^^^^
2 │

Unsafe fix: Use String() call instead.

1 - ``·+·foo;
1+ String(foo);
2 2

foo += "";
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

> 1 │ foo += "";
^^^^^^^^^
2 │

Unsafe fix: Use String() call instead.

1 - foo·+=·"";
1+ foo·=·String(foo);
2 2

~foo.indexOf(1);
code-block.js:1:1 lint/complexity/noImplicitCoercions  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Using binary operations instead of comparisons is harder to read and understand.

> 1 │ ~foo.indexOf(1);
^^^^^^^^^^^^^^^
2 │

Unsafe fix: Compare with -1 instead.

1 - ~foo.indexOf(1);
1+ (foo.indexOf(1)·!==·-1);
2 2

¥Valid

Boolean(foo);
Number(foo);
String(foo);
foo.indexOf(1) !== -1;

这些扩展未被标记,因为它们不执行类型强制转换:

¥These are not flagged because they don’t perform type coercion:

!foo;
~foo;
-foo;
+1234;
2 * foo;
foo + 'bar';

¥Related links