1 # Disallow the type conversion with shorter notations. (no-implicit-coercion)
3 In JavaScript, there are a lot of different ways to convert value types.
4 Some of them might be hard to read and understand.
10 var b = ~foo.indexOf(".");
17 Those can be replaced with the following code:
21 var b = foo.indexOf(".") !== -1;
30 This rule is aimed to flag shorter notations for the type conversion, then suggest a more self-explanatory notation.
34 This rule has three main options and one override option to allow some coercions as required.
36 - `"boolean"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `boolean` type.
37 - `"number"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `number` type.
38 - `"string"` (`true` by default) - When this is `true`, this rule warns shorter type conversions for `string` type.
39 - `"disallowTemplateShorthand"` (`false` by default) - When this is `true`, this rule warns `string` type conversions using `${expression}` form.
40 - `"allow"` (`empty` by default) - Each entry in this array can be one of `~`, `!!`, `+` or `*` that are to be allowed.
42 Note that operator `+` in `allow` list would allow `+foo` (number coercion) as well as `"" + foo` (string coercion).
46 Examples of **incorrect** code for the default `{ "boolean": true }` option:
49 /*eslint no-implicit-coercion: "error"*/
52 var b = ~foo.indexOf(".");
53 // bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
56 Examples of **correct** code for the default `{ "boolean": true }` option:
59 /*eslint no-implicit-coercion: "error"*/
62 var b = foo.indexOf(".") !== -1;
64 var n = ~foo; // This is a just bitwise not.
69 Examples of **incorrect** code for the default `{ "number": true }` option:
72 /*eslint no-implicit-coercion: "error"*/
78 Examples of **correct** code for the default `{ "number": true }` option:
81 /*eslint no-implicit-coercion: "error"*/
84 var n = parseFloat(foo);
85 var n = parseInt(foo, 10);
90 Examples of **incorrect** code for the default `{ "string": true }` option:
93 /*eslint no-implicit-coercion: "error"*/
101 Examples of **correct** code for the default `{ "string": true }` option:
104 /*eslint no-implicit-coercion: "error"*/
110 ### disallowTemplateShorthand
112 This option is **not** affected by the `string` option.
114 Examples of **incorrect** code for the `{ "disallowTemplateShorthand": true }` option:
117 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
122 Examples of **correct** code for the `{ "disallowTemplateShorthand": true }` option:
125 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
133 var s = `${foo}${bar}`;
138 Examples of **correct** code for the default `{ "disallowTemplateShorthand": false }` option:
141 /*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/
148 Using `allow` list, we can override and allow specific operators.
150 Examples of **correct** code for the sample `{ "allow": ["!!", "~"] }` option:
153 /*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/
156 var b = ~foo.indexOf(".");
159 ## When Not To Use It
161 If you don't want to be notified about shorter notations for the type conversion, you can safely disable this rule.