跳转到内容

noImplicitCoercion

此内容尚不支持你的语言。

Disallow shorthand type conversions.

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:

  • Boolean using double negation !!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)
  • String using concatenation with empty string value + "" or empty template literal value +
  • Check index using bitwise NOT with indexOf ~value.indexOf(item) instead of comparing with -1
!!foo;
code-block.js:1:1 lint/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Implicit type conversion is hard to read and understand.

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

Unsafe fix: Use String() call instead.

1 - </strong></span><span style="color: Tomato;"><strong>·+·foo;
1+ String(foo);
2 2

foo += "";
code-block.js:1:1 lint/nursery/noImplicitCoercion  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/nursery/noImplicitCoercion  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

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';
biome.json
{
"linter": {
"rules": {
"nursery": {
"noImplicitCoercion": "error"
}
}
}
}