noImplicitCoercions
¥Summary
-
规则生效日期:
v2.1.0¥Rule available since:
v2.1.0 -
诊断类别:
lint/complexity/noImplicitCoercions¥Diagnostic Category:
lint/complexity/noImplicitCoercions -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-implicit-coercion相同¥Same as
no-implicit-coercion
-
¥How to configure
{ "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、乘以 1value * 1、除以 1value / 1或使用减号-(-value)进行双重取反。¥Number using unary plus
+value, subtraction from zerovalue - 0, multiplication by onevalue * 1, division by onevalue / 1, or double negation with minus-(-value) -
使用空字符串
value + ""或空模板字面量value +进行字符串拼接。¥String using concatenation with empty string
value + ""or empty template literalvalue +“ -
使用
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
Biome v2.1 中文网 - 粤ICP备13048890号