跳至主要內容

prefer-nullish-coalescing

強制使用 nullish 聯合運算子,而不是邏輯賦值或串連。

💡

此規則報告的一些問題可透過編輯器的 建議.

手動修復。

💭 此規則需要 類型資訊

才能執行。

?? nullish 聯合執行階段運算子允許在處理 nullundefined 時提供預設值。因為 nullish 聯合運算子*僅*在原始值為 nullundefined 時才會聯合,所以它比依賴邏輯 OR 運算子串連 || 安全得多,後者會在任何*虛值*上聯合。

  • 此規則會在您可能考慮替換時報告
  • 使用 ?? 替換 || 運算子
使用 ??= 替換 ||= 運算子

注意

如果未啟用 strictNullChecks,則此規則將無法按預期運作。
module.exports = {
"rules": {
"@typescript-eslint/prefer-nullish-coalescing": "error"
}
};

.eslintrc.cjs

在 Playground 中嘗試此規則 ↗

選項

type Options = [
{
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
ignoreConditionalTests?: boolean;
ignoreMixedLogicalExpressions?: boolean;
ignorePrimitives?:
| {
bigint?: boolean;
boolean?: boolean;
number?: boolean;
string?: boolean;
[k: string]: unknown;
}
| true;
ignoreTernaryTests?: boolean;
},
];

const defaultOptions: Options = [
{
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
ignoreConditionalTests: false,
ignoreTernaryTests: false,
ignoreMixedLogicalExpressions: false,
ignorePrimitives: {
bigint: false,
boolean: false,
number: false,
string: false,
},
},
];

此規則接受下列選項

ignoreTernaryTests

將此選項設定為 true 將導致規則忽略任何可以使用 nullish 聯合運算子簡化的三元運算式。預設情況下,此選項設定為 false

const foo: any = 'bar';
foo !== undefined && foo !== null ? foo : 'a string';
foo === undefined || foo === null ? 'a string' : foo;
foo == undefined ? 'a string' : foo;
foo == null ? 'a string' : foo;

const foo: string | undefined = 'bar';
foo !== undefined ? foo : 'a string';
foo === undefined ? 'a string' : foo;

const foo: string | null = 'bar';
foo !== null ? foo : 'a string';
foo === null ? 'a string' : foo;
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

在 Playground 中開啟

const foo: any = 'bar';
foo ?? 'a string';
foo ?? 'a string';
foo ?? 'a string';
foo ?? 'a string';

const foo: string | undefined = 'bar';
foo ?? 'a string';
foo ?? 'a string';

const foo: string | null = 'bar';
foo ?? 'a string';
foo ?? 'a string';
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

ignoreTernaryTests: false 的正確程式碼

ignoreConditionalTests

將此選項設定為 true 將導致規則忽略位於條件測試中的任何案例。預設情況下,此選項設定為 false

通常,條件測試中的運算式會故意使用邏輯 OR 運算子的虛值穿透行為,這表示將運算子修復為 nullish 聯合運算子可能會導致錯誤。

如果您希望強制執行更嚴格的條件測試,則應考慮使用 strict-boolean-expressions 規則。

declare const a: string | null;
declare const b: string | null;

if (a || b) {
}
if ((a ||= b)) {
}
while (a || b) {}
while ((a ||= b)) {}
do {} while (a || b);
for (let i = 0; a || b; i += 1) {}
a || b ? true : false;
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

ignoreConditionalTests: false 的錯誤程式碼,以及 ignoreConditionalTests: true 的正確程式碼

declare const a: string | null;
declare const b: string | null;

if (a ?? b) {
}
if ((a ??= b)) {
}
while (a ?? b) {}
while ((a ??= b)) {}
do {} while (a ?? b);
for (let i = 0; a ?? b; i += 1) {}
a ?? b ? true : false;
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

ignoreConditionalTests: false 的正確程式碼

ignoreMixedLogicalExpressions

將此選項設定為 true 將導致規則忽略任何屬於混合邏輯運算式(包含 &&)一部分的邏輯 OR 運算式。預設情況下,此選項設定為 false

通常,條件測試中的運算式會故意使用邏輯 OR 運算子的虛值穿透行為,這表示將運算子修復為 nullish 聯合運算子可能會導致錯誤。

通常,混合邏輯運算式中的運算式會故意使用邏輯 OR 運算子的虛值穿透行為,這表示將運算子修復為 nullish 聯合運算子可能會導致錯誤。

declare const a: string | null;
declare const b: string | null;
declare const c: string | null;
declare const d: string | null;

a || (b && c);
a ||= b && c;
(a && b) || c || d;
a || (b && c) || d;
a || (b && c && d);
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

ignoreMixedLogicalExpressions: false 的錯誤程式碼,以及 ignoreMixedLogicalExpressions: true 的正確程式碼

declare const a: string | null;
declare const b: string | null;
declare const c: string | null;
declare const d: string | null;

a ?? (b && c);
a ??= b && c;
(a && b) ?? c ?? d;
a ?? (b && c) ?? d;
a ?? (b && c && d);
ignoreTernaryTests: false 的錯誤程式碼,以及 ignoreTernaryTests: true 的正確程式碼

ignoreMixedLogicalExpressions: false 的正確程式碼

**_注意:_** 此特定案例的錯誤將以建議(如下所示)而非修復的形式呈現。這是因為在混合邏輯運算式中自動將 || 轉換為 ?? 並不總是安全的,因為我們無法判斷運算子的預期優先順序。請注意,根據設計,在同一個運算式中將 ??&&|| 一起使用時,需要使用括弧。

ignorePrimitives

  • 如果您希望忽略包含可能是虛值的特定基本類型運算元的運算式,則可以傳遞一個物件,其中包含每個基本類型的布林值
  • string: true,忽略與 stringnullundefined 聯集(預設值:false)。
  • number: true,忽略與 numbernullundefined 聯集(預設值:false)。
  • bigint: true,忽略與 bigintnullundefined 聯集(預設值:false)。

boolean: true,忽略與 booleannullundefined 聯集(預設值:false)。

ignorePrimitives: { string: false } 的錯誤程式碼,以及 ignorePrimitives: { string: true } 的正確程式碼

ignorePrimitives: { string: false }ignorePrimitives: { string: true } 的正確程式碼

此外,如果您希望忽略所有基本類型,則可以設定 ignorePrimitives: true。這等效於 ignorePrimitives: { string: true, number: true, bigint: true, boolean: true }

allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing

如果此選項設定為 false,則規則會在每個 tsconfig.json*未*將 strictNullChecks 編譯器選項(或 strict)設定為 true 的檔案上出錯。

如果沒有 strictNullChecks,TypeScript 本質上會從類型中抹除 undefinednull。這表示當此規則檢查變數的類型時,*將無法判斷變數可能是 nullundefined*,這基本上會使此規則失效。

您應該使用 strictNullChecks 來確保程式碼庫中的完整類型安全。

如果由於某些原因您無法啟用 strictNullChecks,但仍然想使用此規則,則可以使用此選項來允許它,但請注意,如果關閉編譯器選項,則此規則的行為*未定義*。如果您使用此選項,我們將不接受錯誤報告。

何時不應使用

如果您沒有使用 TypeScript 3.7(或更新版本),則將無法使用此規則,因為不支援該運算子。


類型檢查的 lint 規則比傳統的 lint 規則更強大,但也需要設定類型檢查 linting。如果您在啟用類型檢查規則後遇到效能下降的問題,請參閱效能疑難排解

資源