Closed
Description
It'd be great to have a lint rule that disallows (possible) strings and numbers from being implicitly converted to booleans:
const str: string | null = ''
if (str) {
// ignores the empty string, the programmer likely attempted to guard against null
}
const num: number | undefined = 0
if (!num) {
// ignores the 0 value, but the programmer likely attempted to guard against undefined
}
Besides if
statements this would apply to ternary statements as well as &&
, ||
and !
operators.
For the above examples passing code could be:
const str: string | null = ''
if (typeof str === 'string') {
// …
}
if (str !== null) {
// …
}
const num: number | undefined = 0
if (typeof num !== 'undefined') {
// ...
}
I can see a stricter version of this rule which enforces explicit conversions even if a value can only be a string or number.