no-implicit-coercion
Configuration
rslint.config.ts
Disallow shorthand type conversions in favor of explicit Boolean() / Number() / String() calls.
Rule Details
In JavaScript, type conversions are often performed using shorthand syntax. While these idioms work, they obscure intent; the rule flags them so they can be replaced with explicit conversions.
Patterns flagged:
!!fooinstead ofBoolean(foo)~foo.indexOf(bar)(orlastIndexOf) instead offoo.indexOf(bar) !== -1+fooor-(-foo)instead ofNumber(foo)1 * fooorfoo * 1instead ofNumber(foo)foo - 0instead ofNumber(foo)"" + foo/foo + ""(including``) instead ofString(foo)foo += ""instead offoo = String(foo)- Template shorthand
`${foo}`instead ofString(foo)— only when thedisallowTemplateShorthandoption is enabled.
Options
boolean(defaulttrue) — disallow boolean shorthand conversions (!!,~).number(defaulttrue) — disallow numeric shorthand conversions (+,- -,- 0,* 1).string(defaulttrue) — disallow string shorthand conversions ("" +,+= "").disallowTemplateShorthand(defaultfalse) — also disallow`${foo}`as a coercion.allow— operators to exempt from the above. Allowed values:"~","!!","+","- -","-","*".
Fix vs suggestion
Only !!foo → Boolean(foo) applies as an autofix (and only when Boolean is not shadowed in scope). The other rewrites are offered as suggestions because they can change runtime behavior — Number(1n) throws, foo.indexOf(x) !== -1 differs from ~foo.indexOf(x) on non-array targets, etc.
Examples
Incorrect:
Correct: