src/content/docs/linter/rules/no-implicit-coercions.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.1.0` - Diagnostic Category: [`lint/complexity/noImplicitCoercions`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has an [**unsafe**](/linter/#unsafe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-implicit-coercion`](https://eslint.org/docs/latest/rules/no-implicit-coercion){
"linter": {
"rules": {
"complexity": {
"noImplicitCoercions": "error"
}
}
}
}
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:
!!value+value, subtraction from zero value - 0, multiplication by one value * 1, division by one value / 1, or double negation with minus -(-value)value + "" or empty template literal value + ``~value.indexOf(item) instead of comparing with -1!!foo;
+foo;
-(-foo);
foo - 0;
foo * 1;
foo / 1;
"" + foo;
foo + "";
`` + foo;
foo += "";
~foo.indexOf(1);
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';