⇒ explicit conversion between types

JavaScript pattern

Use explicit conversions between types, e.g., '' + x => String(s).


Apply with the Grit CLI
grit apply explicit_type_conversion

Handles string preceding variable

BEFORE
var x = '' + foo;
AFTER
var x = String(foo);

Handles string following variable

BEFORE
var x = a + '';
AFTER
var x = String(a);

Handles interpolated string

BEFORE
var x = foo + '' + bar;
AFTER
var x = String(foo) + bar;

Handles number conversion using +

BEFORE
var x = +foo;
AFTER
var x = Number(foo);

Handles number conversion using *

BEFORE
var x = 1 * foo;
AFTER
var x = Number(foo);

Handles boolean conversion using !!

BEFORE
var x = !!"123";
AFTER
var x = Boolean("123");