no-implicit-coercion
禁止速记类型转换
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
在 JavaScript 中,有很多不同的方法可以转换值类型。其中一些可能难以阅读和理解。
如:
var b = !!foo;
var b = ~foo.indexOf(".");
var n = +foo;
var n = 1 * foo;
var s = "" + foo;
foo += ``;
这些可以替换为以下代码:
var b = Boolean(foo);
var b = foo.indexOf(".") !== -1;
var n = Number(foo);
var n = Number(foo);
var s = String(foo);
foo = String(foo);
规则详情
7gPduFyJLZE21664V/7Nz4/ggbV50hbtHesrlJ1P74VD6XytPN0FR6OeGPrmA6ypLf387OunA7X1OkFS9yofhUDhuVwKacIBIrb+8UfBZBtRyqfOvOcxJsBbeA7gAT/X
选项
VfdvOgFCXxbIe8FnTIbc0SNYb13vsH3nhA4ggDai/MDp1t0I+sDr03pR9H0/sNmGD7c/yQrpsNUo+yVYSkn7QuFTTGuPaCCxEhO7mUiRllW5luyKLnTm+6IKFbY29VYNPOhbPH8Y6YDkQK/jwwRRSg==
- onhsCx3bxnAVIQ3lM0HvEe9BCvu28xFl2uRe/91O52EUxP9d3pH4CNMgN8ywqCT0GADEWv81AV30YpRWLwI5Vie0yIesolfhKOhsigVd2VCOzziXOscIBTqg/lplGJJWDIXwJHEgfChQqZjz0fBN4v4bOUnCqGPN+nAqKMswcplfgxAzjLPq1gBtId6Rjgbljt5ys96gf/NnFSjkHMBnf3IJjmUu6ui1vcoSgxAsEDlWLOBoS0lYPg/yF6esGzO0exOlFwemXmyBZxKQGFp0kS8/drFuiRTewGvXOlk/vsViU6l0IUg3ke/3v9zmsbSeg8Pb+ilzY7LUzW6F6rXT/M3E3c/Uv+1G61R6NPzYwkUnr70cgy9MqdjBJB6qHxc9fgUGboGtUWSOjzWloEzIzsAlE1AFYm+VVh09nG/CufFZI7lEVJDWczKknaAQApF3YN+e4hPvZwWXHuSq+KCQX2ziHOhdqiWq7xhMA2DXBBC52afjqtmKJOdBhS++Oq8PWOJPfSmIDgxSWG5r+2mcHVDusgdAY6iahuUBut99t/PVsvbmOekiUW0BHUiEtf3zG93MZqJVvTB4s/ULBP+zGbFOSw80JRNHtoGk13iDY104KeN2HPehpRHaEkXXTJknlQwaJJiobs12YR3or55ohBHUSGedtxCuU4FJr5wyKtB/xYDTQzGeoiokK5zUGPCHVa1ruw13E9SLX9hy9841fu/Dz0tBEHFRKGB8qtKti0sIOnIclUZnc2dR5JgNUZQol45aQGtYdpuT2HkG47PwKgUJnf/qPdcjwiv4b0wQ0ZMCTmRid6FbR31Afy6dSOnX1vS5e4GAvarSclWfmb9QmzUIGQnlC8I5QCzxXlVbhdID/eD/8YGvRgdhtetEVfifts2TWC0VyW2yCKw22aBGjsxgShC7hSbUnhCaOODJ5HZq1rr8jC4rzjdppwwkRSwXP4VZae7/T8b12g1jDcctYKKprVlzOVtJLv5LczGNu2feftO1zIT4B3aMc6DJeSYJnc8jRvrVffDp39WHGykO+ujjWTZ0ADtNEnL92emOuvfuRMgtII2HPXM1m723H1RqzJ578QFb8UgvWMJ7HpcYH3WHtfI8dn3S4gun1kHgFw2bIpZtVzrbVrjOzmclAZEA4pC7iR2vKEVnI2rmPhXoD8pPBtjMIPUXSy/PDydRkxEfzFo2NV+zaqkKST047LOW
+Giv2kBQElkcxJplDBxnN92ZX7ketu1No959yqSF2Y/uTMaO6VcJbgCnN0HVd/tSSQPzKH0cZInjhbNtCUSWX0h7GHgS1PQZYygqBSoEj7Oj6iwTQKpnBpt5ybExjE8eETuitwH/AsR62xexoznnIervfWZjEV2e0BuYO/2Mq79Yw+b5XLN5QhNJX874CDSMIHb6DJaEbpy4xu3Lz2IV3OLLM+kZgOV7f1lVExlT+m8=
boolean
guGHUWtl5nT+Of2zkH6EkODVfhTCuqcnhujkvNo0QfjK5R8Ry4EVJ0ix8ovtDruSVlfQEL2W/c6lI8kLRD11eYYtOzBrPw+RYSpEbThDMcI=
/*eslint no-implicit-coercion: "error"*/
var b = !!foo;
var b = ~foo.indexOf(".");
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.
guGHUWtl5nT+Of2zkH6EkODVfhTCuqcnhujkvNo0QfjK5R8Ry4EVJ0ix8ovtDruStfSu9LdmXeDkITHSqAslLO9UhT1C47qF0z+O4awjcn0=
/*eslint no-implicit-coercion: "error"*/
var b = Boolean(foo);
var b = foo.indexOf(".") !== -1;
var n = ~foo; // This is a just bitwise not.
number
guGHUWtl5nT+Of2zkH6EkE9BLXsFea4v/ZKc5a7vQexe63Wub4gnqOupZ1RjfrJOM/ge/P9qV/Z8XV2Bugk248EXmGZ2JyybXdZz4qqhHPY=
/*eslint no-implicit-coercion: "error"*/
var n = +foo;
var n = 1 * foo;
guGHUWtl5nT+Of2zkH6EkE9BLXsFea4v/ZKc5a7vQexe63Wub4gnqOupZ1RjfrJOZX8fbIRt7nUursEXt90/ur0+Y2I/7+7Pw2LWkIbhXYo=
/*eslint no-implicit-coercion: "error"*/
var n = Number(foo);
var n = parseFloat(foo);
var n = parseInt(foo, 10);
var n = foo * 1/4; // `* 1` is allowed when followed by the `/` operator
string
guGHUWtl5nT+Of2zkH6EkGhJvBQIW6fUbGdfjP/4ZlVo2Yn+cqkQZrkA3bYB3jrnFlIHtnyf1LX7qz/uXA/Ucz+aJLFOC/qbTeTaUzKRhkY=
/*eslint no-implicit-coercion: "error"*/
var s = "" + foo;
var s = `` + foo;
foo += "";
foo += ``;
guGHUWtl5nT+Of2zkH6EkGhJvBQIW6fUbGdfjP/4ZlVo2Yn+cqkQZrkA3bYB3jrnlxUlyDywuLiArocg1kUC3V+nC9eFLwi664/OJ0yZmrY=
/*eslint no-implicit-coercion: "error"*/
var s = String(foo);
foo = String(foo);
disallowTemplateShorthand
ffFTu5oYz0lHjZCicFgozCT6XXL9JTKfed7llHKtInELk3qgI5LseHVA/FAGGVvfT+TQ2hrb6lnC2fz2iNjodQ==
YPuexw+CdjpbR16dLvBF8yukB0WQhq0wocteKK8t5qwdCVmOqUQ4aIsIPHusCrC5NrwBu64qpH6+grpL9RQEO+n2y7AedQUrIurkfgnwSvmIyHgrW3/KXtOgfheasyjW
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
var s = `${foo}`;
YPuexw+CdjpbR16dLvBF8yukB0WQhq0wocteKK8t5qwdCVmOqUQ4aIsIPHusCrC5LAfbGxwqGicrB5D4g/qs8dqMel0/m4m8m8DV+GvA4Y06d4dgFa+Go6hSKO3gVpBt
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/
var s = String(foo);
var s = `a${foo}`;
var s = `${foo}b`;
var s = `${foo}${bar}`;
var s = tag`${foo}`;
guGHUWtl5nT+Of2zkH6EkJEGSrl4pNI42GOdxCVVBwNzFMRlfW9KGzHis7HgpFDoUhW3e41Ap6IT3EurnPea8d8saJ1Fcs82UiBkf+hPqdaxL2wsP3HARNx+NGchJej9
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/
var s = `${foo}`;
allow
UDlPfahVcI+Ts9tnO1dFvUQwbSfovfjKfhs8oip3Tw5D5ytImk2J41DZmY2Mwl+eL2H01rWd2iC+9bBvsYn09u9OuOA+3QbY7D628jXlBicG+iYBjGZOf3H7KOfsU1F/
CHgSPA5wZV2aphfLOB4KLwzz/XPwmMHJLNCck1N6QuaORbqCRDCS9RtqpihBQF1W5tTYYGcY+uDfGgeWsx2453tzfdwCloxVuqpG2FUXyqY=
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/
var b = !!foo;
var b = ~foo.indexOf(".");
何时不使用
eVV5gQ3XUB4iwZJDgcbIkOYm74NB/YS5r4+VrhCkLCpBP1jXJTLTpIpI3w7d7PpLoCu9WxouoXhPHbz+vDxIaQCdAEBuur4ybhbaythIWRSYOTZIh+koDHKcg5tUFn3vZ+W3N7lhLe1goM5Ls+tuiA==