I recently had an issue regarding a ternary checking a number | undefined var for undefined, but due to my lack of attention when writing the code, when the number was 0, it wrongly accused it being a undefined value.
Then, I found about about the strict-boolean-expressions ESLint rule.
Looks very useful and safe, but, given this example:
const text: string | undefined = stringOrUndefined1 || stringOrUndefined2 || undefined; // the strings can be empty
if (!text) // I was doing it this way to check if the value was falsy. With the new rule, it complains.
return;
if (text === undefined || text === '') // This works, but is 4x the length of the one above. I don't want to write the var name more than once
if (!!text == false) // "Unexpected nullable string value in conditional. Please handle the nullish/empty cases explicitly."
if (!!!text) // Same warn as above
Is there any way to quickly and nicely check if the value is falsy without the lengthy second conditional, and without disabling the rule, even just for the specific line?
I know that it's possible to disable just for nullable strings, but this question also applies for numbers, the rule that I want to keep for safety reasons.