Is there a TSLint/ESLint rule to prevent unnecessary boolean comparison like:
if (result === false) {
// do something
}
Is there a TSLint/ESLint rule to prevent unnecessary boolean comparison like:
if (result === false) {
// do something
}
TSLint supports rule no-boolean-literal-compare
which does just this.
"no-boolean-literal-compare": true
in the rules
array will enable this feature.
Documentation link: https://palantir.github.io/tslint/rules/no-boolean-literal-compare/
You can currently accomplish this with ESLint:
"no-restricted-syntax": [
"error",
{
"selector": "BinaryExpression[operator=/^(==|===|!=|!==)$/][left.raw=/^(true|false)$/], BinaryExpression[operator=/^(==|===|!=|!==)$/][right.raw=/^(true|false)$/]",
"message": "Don't compare for equality against boolean literals"
}
]
The selector disallows usage of ==
, ===
, !=
, and !==
when either (or both) of the operands are boolean literals.
© 2022 - 2024 — McMap. All rights reserved.