Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I have searched for related issues and found none that match my proposal.
- I have searched the current rule list and found no rules that match my proposal.
- I have read the FAQ and my problem is not listed.
My proposal is suitable for this project
- I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).
Link to the rule's documentation
https://typescript-eslint.io/rules/strict-boolean-expressions/
Description
Hello! We believe strongly that strict boolean expressions are a good thing, because truthy evaluation opens the door for developer error and bugs to slip into the wild.
For this reason, we very much like the power of the strict-boolean-expressions rule.
The challenge we are facing, is how auto-fix conflicts with some other rules that we believe are very valuable:
- eqeqeq - We agree with the philosophy of the rule: It is considered good practice to use the type-safe equality operators === and !== instead of their regular counterparts == and !=. The reason for this is that == and != do type coercion.
- no-eq-null - Comparing to null without a type-checking operator (== or !=), can have unintended results as the comparison will evaluate to true when comparing to not just a null, but also an undefined value.
The fixes and suggestions (specifically the auto-fix for objects when allowNullableObject
is false
) conflict with the above rules.
I am sure that there is good reason and a lot of discussion behind why there was a decision to use == null
as opposed to === undefined
, but I thought it would be worth opening a discussion to see if we can get a consensus that it would be preferable to change the behavior to align more with the core eslint rules and the philosophy behind why it is a good idea to enable them.
Fail
let obj;
if (Math.random() > 0.5) {
obj = {};
}
if (obj) {
console.log('I am set!');
}
Pass
// Current behavior
let obj;
if (Math.random() > 0.5) {
obj = {};
}
if (obj != null) {
console.log('I am set!');
}
// Preferred behavior
let obj;
if (Math.random() > 0.5) {
obj = {};
}
if (obj !== undefined) {
console.log('I am set!');
}
Additional Info
-
There are a couple issues I found with some information on the topic, but it is not explicit about this particular choice so I thought it was worth opening a new thread: