Skip to content

Enhancement: [strict-boolean-expressions] Change == to ===? #6173

Closed as not planned
@justinhelmer

Description

@justinhelmer

Before You File a Proposal Please Confirm You Have Done The Following...

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:

  1. 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.
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions