版本

no-implicit-coercion

不允許簡寫類型轉換

🔧 可修正

此規則報告的某些問題可以透過 --fix 命令列 選項自動修正

💡 有建議

此規則報告的某些問題可以透過編輯器建議手動修正

❄️ 已凍結

此規則目前為凍結狀態,不接受功能請求。

在 JavaScript 中,有很多不同的方式可以轉換值類型。其中一些可能難以閱讀和理解。

例如

const b = !!foo;
const b1 = ~foo.indexOf(".");
const n = +foo;
const n1 = -(-foo);
const n2 = foo - 0;
const n3 = 1 * foo;
const s = "" + foo;
foo += ``;

這些可以替換為以下程式碼

const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;
const n = Number(foo);
const n1 = Number(foo);
const n2 = Number(foo);
const n3 = Number(foo);
const s = String(foo);
foo = String(foo);

規則詳細資訊

此規則旨在標記較短的類型轉換符號,然後建議更易於理解的符號。

選項

此規則有三個主要選項和一個覆寫選項,可以根據需要允許某些強制轉換。

  • "boolean" (預設為 true) - 當此選項為 true 時,此規則會警告 boolean 類型的較短類型轉換。
  • "number" (預設為 true) - 當此選項為 true 時,此規則會警告 number 類型的較短類型轉換。
  • "string" (預設為 true) - 當此選項為 true 時,此規則會警告 string 類型的較短類型轉換。
  • "disallowTemplateShorthand" (預設為 false) - 當此選項為 true 時,此規則會警告使用 ${expression} 形式的 string 類型轉換。
  • "allow" (預設為 empty) - 此陣列中的每個條目可以是 ~!!+- --* 之一,這些運算符將被允許。

請注意,allow 列表中的運算符 + 將允許 +foo (數字強制轉換) 以及 "" + foo (字串強制轉換)。

布林值

預設 { "boolean": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const b = !!foo;
const b1 = ~foo.indexOf(".");
// bitwise not is incorrect only with `indexOf`/`lastIndexOf` method calling.

預設 { "boolean": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const b = Boolean(foo);
const b1 = foo.indexOf(".") !== -1;

const n = ~foo; // This is a just bitwise not.

數字

預設 { "number": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const n = +foo;
const n1 = -(-foo);
const n2 = foo - 0;
const n3 = 1 * foo;

預設 { "number": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const n = Number(foo);
const n1 = parseFloat(foo);
const n2 = parseInt(foo, 10);

const n3 = foo * 1/4; // `* 1` is allowed when followed by the `/` operator

字串

預設 { "string": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const s = "" + foo;
const s1 = `` + foo;
foo += "";
foo += ``;

預設 { "string": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: "error"*/

const s = String(foo);
foo = String(foo);

disallowTemplateShorthand

此選項不受 string 選項的影響。

{ "disallowTemplateShorthand": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

const s = `${foo}`;

{ "disallowTemplateShorthand": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": true }]*/

const s = String(foo);

const s1 = `a${foo}`;

const s2 = `${foo}b`;

const s3 = `${foo}${bar}`;

const s4 = tag`${foo}`;

預設 { "disallowTemplateShorthand": false } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: ["error", { "disallowTemplateShorthand": false }]*/

const s = `${foo}`;

允許

使用 allow 列表,我們可以覆寫並允許特定的運算符。

範例 { "allow": ["!!", "~"] } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-implicit-coercion: [2, { "allow": ["!!", "~"] } ]*/

const b = !!foo;
const b1 = ~foo.indexOf(".");

何時不該使用

如果您不希望收到關於類型轉換的較短符號的通知,您可以安全地停用此規則。

版本

此規則在 ESLint v1.0.0-rc-2 中引入。

資源

變更語言