嚴謹的布林運算式
不允許布林運算式中的特定類型。
禁止在應使用布林值的運算式中使用非布林類型。boolean
和 never
類型始終被允許。額外視為在布林文中安全的類型,可透過選項進行設定。
以下節點被視為布林運算式,並檢查其類型
- 邏輯否定的運算子 (
!arg
) 引數。 - 條件運算式 (
cond ? x : y
) 中的條件。 if
、for
、while
和do-while
陳述式的條件。- 邏輯二元運算子 (
lhs || rhs
和lhs && rhs
) 的運算元。- 當它不是其他布林運算式的後代時,右側運算元會被忽略。這是為了允許基於其短路行為使用布林運算子。
module.exports = {
"rules": {
"@typescript-eslint/strict-boolean-expressions": "error"
}
};
在練習區嘗試此規則 ↗
範例
- ❌ 不正確
- ✅ 正確
// nullable numbers are considered unsafe by default
let num: number | undefined = 0;
if (num) {
console.log('num is defined');
}
// nullable strings are considered unsafe by default
let str: string | null = null;
if (!str) {
console.log('str is empty');
}
// nullable booleans are considered unsafe by default
function foo(bool?: boolean) {
if (bool) {
bar();
}
}
// `any`, unconstrained generics and unions of more than one primitive type are disallowed
const foo = <T>(arg: T) => (arg ? 1 : 0);
// always-truthy and always-falsy types are disallowed
let obj = {};
while (obj) {
obj = getObj();
}
在遊樂場開啟// nullable values should be checked explicitly against null or undefined
let num: number | undefined = 0;
if (num != null) {
console.log('num is defined');
}
let str: string | null = null;
if (str != null && !str) {
console.log('str is empty');
}
function foo(bool?: boolean) {
if (bool ?? false) {
bar();
}
}
// `any` types should be cast to boolean explicitly
const foo = (arg: any) => (Boolean(arg) ? 1 : 0);
在遊樂場開啟選項
此規則接受以下選項
type Options = [
{
allowAny?: boolean;
allowNullableBoolean?: boolean;
allowNullableEnum?: boolean;
allowNullableNumber?: boolean;
allowNullableObject?: boolean;
allowNullableString?: boolean;
allowNumber?: boolean;
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing?: boolean;
allowString?: boolean;
},
];
const defaultOptions: Options = [
{
allowString: true,
allowNumber: true,
allowNullableObject: true,
allowNullableBoolean: false,
allowNullableString: false,
allowNullableNumber: false,
allowNullableEnum: false,
allowAny: false,
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing: false,
},
];
allowString
在布林情境中允許 string
。此舉很安全,因為字串只有一個偽值 (""
)。如果你偏好明確的 str != ""
或 str.length > 0
樣式,請將其設定為 false
。
allowNumber
在布林情境中允許 number
。此舉很安全,因為數字只有兩個偽值 (0
和 NaN
)。如果你偏好明確的 num != 0
和 !Number.isNaN(num)
樣式,請將其設定為 false
。
allowNullableObject
在布林情境中允許 object | function | symbol | null | undefined
。此舉很安全,因為物件、函式和符號沒有偽值。如果你偏好明確的 obj != null
樣式,請將其設定為 false
。
allowNullableBoolean
在布林情境中允許 boolean | null | undefined
。此舉很危險,因為允許為空的布林可能是 false
或 null。如果你想要強制明確的 bool ?? false
或 bool ?? true
樣式,請將其設定為 false
。如果你不介意隱含地將 false 視為 null 值,請將其設定為 true
。
allowNullableString
在布林情境中允許 string | null | undefined
。此舉很危險,因為允許為空的字串可能是空字串或 null。如果你不介意隱含地將空字串視為 null 值,請將其設定為 true
。
allowNullableNumber
在布林情境中允許 number | null | undefined
。此舉很危險,因為允許為空的數字可能是偽數字或 null。如果你不介意隱含地將 0 或 NaN 視為 null 值,請將其設定為 true
。
allowNullableEnum
允許在布林值中使用 enum | null | undefined
。這是不安全的,因為可為 null 的列舉可能是假數值或 nullish。如果您不介意隱含將值為零的列舉視為與 nullish 值相同,請將此值設定為 true
。
allowAny
允許在布林值中使用 any
。這顯然是不安全的。請自行承擔將此值設定為 true
的風險。
allowRuleToRunWithoutStrictNullChecksIKnowWhatIAmDoing
如果將此值設定為 false
,則此規則會在每個 tsconfig.json
未將編譯器選項 strictNullChecks
(或 strict
)設定為 true
的檔案中造成錯誤。
在未使用 strictNullChecks
的情況下,TypeScript 基本上會從類型中移除 undefined
和 null
。這意味著,當此規則檢查變數的類型時,無法得知變數可能是 null
或 undefined
,這基本讓此規則變得不那麼有用。
您應使用 strictNullChecks
,以確保程式碼庫中的類型安全性完整。
如果您基於某些原因無法啟用 strictNullChecks
,但仍想使用此規則,則可以使用此選項允許使用該規則,但請注意,如果關閉編譯器選項,此規則的行為是未定義的。如果您使用此選項,我們不接受問題回報。
修正和建議
此規則提供下列修正和建議,適用於布林值中的特定類型
boolean
- 始終允許 - 無需修正。string
-(當allowString
為false
時)- 提供下列建議- 變更條件,用以檢查字串長度 (
str
→str.length > 0
) - 變更條件,用以檢查空字串 (
str
→str !== ""
) - 將值明確轉型為布林值 (
str
→Boolean(str)
)
- 變更條件,用以檢查字串長度 (
number
-(當allowNumber
為false
時)- 對於
array.length
- 提供自動修正- 變更條件,用以檢查 0 (
array.length
→array.length > 0
)
- 變更條件,用以檢查 0 (
- 對於其他數值 - 提供下列建議
- 變更條件,用以檢查 0 (
num
→num !== 0
) - 變更條件,用以檢查 NaN (
num
→!Number.isNaN(num)
) - 將值明確轉型為布林值 (
num
→Boolean(num)
)
- 變更條件,用以檢查 0 (
- 對於
物件 | null | undefined
- (當allowNullableObject
為false
時) - 提供自動修正- 變更條件為檢查 null/undefined (
maybeObj
→maybeObj != null
)
- 變更條件為檢查 null/undefined (
布林 | null | undefined
- 提供下列建議事項- 將 null 值明確視為 false (
maybeBool
→maybeBool ?? false
) - 變更條件為檢查 true/false (
maybeBool
→maybeBool === true
)
- 將 null 值明確視為 false (
字串 | null | undefined
- 提供下列建議事項- 變更條件為檢查 null/undefined (
maybeStr
→maybeStr != null
) - 將 null 值明確視為空字串 (
maybeStr
→maybeStr ?? ""
) - 明確將值轉換為布林 (
maybeStr
→Boolean(maybeStr)
)
- 變更條件為檢查 null/undefined (
數字 | null | undefined
- 提供下列建議事項- 變更條件為檢查 null/undefined (
maybeNum
→maybeNum != null
) - 將 null 值明確視為 0 (
maybeNum
→maybeNum ?? 0
) - 明確將值轉換為布林 (
maybeNum
→Boolean(maybeNum)
)
- 變更條件為檢查 null/undefined (
任何
和未知
- 提供下列建議事項- 明確將值轉換到布林 (
值
→Boolean(值)
)
- 明確將值轉換到布林 (
何時不應使用它
如果專案不太可能在邏輯條件中使用錯誤的非布林值而導致錯誤,則可以略過啟用此規則。
否則,對於要求在邏輯檢查中進行精確比較,這個規則可能非常嚴格。如果您偏好更簡潔的檢查,而不是更精確的布林邏輯,則此規則可能不適合您。
相關
- no-unnecessary-condition - 報告條件中永遠為真和永遠為假的值的類似規則
類型檢查的 Lint 規則比傳統的 Lint 規則更強大,但還需要設定類型檢查的 Lint。如果您在啟用類型檢查規則後發現效能下降,請參閱效能問題排除。