noImplicitCoercions
Este conteúdo não está disponível em sua língua ainda.
Summary
Section titled “Summary”- Rule available since: 
v2.1.0 - Diagnostic Category: 
lint/complexity/noImplicitCoercions - This rule has an unsafe fix.
 - The default severity of this rule is information.
 - Sources:
- Same as 
no-implicit-coercion 
 - Same as 
 
How to configure
Section titled “How to configure”{  "linter": {    "rules": {      "complexity": {        "noImplicitCoercions": "error"      }    }  }}Description
Section titled “Description”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 zerovalue - 0, multiplication by onevalue * 1, division by onevalue / 1, or double negation with minus-(-value) - String using concatenation with empty string 
value + ""or empty template literalvalue +“ - Check index using bitwise NOT with indexOf 
~value.indexOf(item)instead of comparing with -1 
Examples
Section titled “Examples”Invalid
Section titled “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 │   
  
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
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.