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-while
statements.
Notes:
TS Only
Requires Type Info
Config
These options may be provided:
allow-null-union
allows union types containingnull
.- It does not allow
null
itself. - Without the ‘–strictNullChecks’ compiler option, this will allow anything other than a string, number, or enum.
- It does not allow
allow-undefined-union
allows union types containingundefined
.- It does not allow
undefined
itself. - Without the ‘–strictNullChecks’ compiler option, this will allow anything other than a string, number, or enum.
- It does not allow
allow-string
allows strings.- It does not allow unions containing
string
. - It does not allow string literal types.
- It does not allow unions containing
allow-enum
allows enums.- It does not allow unions containing
enum
.
- It does not allow unions containing
allow-number
allows numbers.- It does not allow unions containing
number
. - It does not allow enums or number literal types.
- It does not allow unions containing
allow-mix
allows multiple of the above to appear together.- For example,
string | number
orRegExp | null | undefined
would normally not be allowed. - A type like
"foo" | "bar" | undefined
is always allowed, because it has only one way to be false.
- For example,
allow-boolean-or-undefined
allowsboolean | 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-rhs
ignores 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 }