Rule: strict-boolean-expressions
Restricts the types allowed in boolean expressions. By default only booleans are allowed.
The following nodes are checked:
- Arguments to the
!,&&, and||operators - The condition in a conditional expression (
cond ? x : y) - Conditions for
if,for,while, anddo-whilestatements.
Notes:
TS Only
Requires Type Info
Config
These options may be provided:
allow-null-unionallows union types containingnull.- It does not allow
nullitself. - Without the ‘–strictNullChecks’ compiler option, this will allow anything other than a string, number, or enum.
- It does not allow
allow-undefined-unionallows union types containingundefined.- It does not allow
undefineditself. - Without the ‘–strictNullChecks’ compiler option, this will allow anything other than a string, number, or enum.
- It does not allow
allow-stringallows strings.- It does not allow unions containing
string. - It does not allow string literal types.
- It does not allow unions containing
allow-enumallows enums.- It does not allow unions containing
enum.
- It does not allow unions containing
allow-numberallows numbers.- It does not allow unions containing
number. - It does not allow enums or number literal types.
- It does not allow unions containing
allow-mixallows multiple of the above to appear together.- For example,
string | numberorRegExp | null | undefinedwould normally not be allowed. - A type like
"foo" | "bar" | undefinedis always allowed, because it has only one way to be false.
- For example,
allow-boolean-or-undefinedallowsboolean | undefined.- Also allows
true | false | undefined. - Does not allow
false | undefined. - This option is a subset of
allow-undefined-union, so you don’t need to enable both options at the same time.
- Also allows
ignore-rhsignores the right-hand operand of&&and||.
Config examples
"strict-boolean-expressions": true
"strict-boolean-expressions": [ true, "allow-null-union", "allow-undefined-union", "allow-string", "allow-enum", "allow-number" ]
"strict-boolean-expressions": [true, "allow-boolean-or-undefined"]
Schema
{
"type": "array",
"items": {
"type": "string",
"enum": [
"allow-null-union",
"allow-undefined-union",
"allow-string",
"allow-enum",
"allow-number",
"allow-boolean-or-undefined",
"ignore-rhs"
]
},
"minLength": 0,
"maxLength": 7
}