Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enhancement: [@typescript-eslint/no-unnecessary-condition] Detect unnecessary comparison to falsy values #5021

Closed
4 tasks done
Samuel-Therrien-Beslogic opened this issue May 20, 2022 · 2 comments
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@Samuel-Therrien-Beslogic
Copy link
Contributor

Samuel-Therrien-Beslogic commented May 20, 2022

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/no-unnecessary-condition/

Description

I would like unnecessary falsy values checks when the variable is already checked for truthiness in the same condition chain to be raised. I often see newer devs not realize that their condition is redundant.

Fail

const myVar = '' as string | number;

if (myVar && myVar !== '') {
  // do something
}

if (myVar && myVar !== 0) {
  // do something
}

if (myVar && !!myVar ) {
  // do something
}

Pass

const myVar = '' as string | number;

if (myVar) {
  // do something
}

Additional Info

No response

@Samuel-Therrien-Beslogic Samuel-Therrien-Beslogic added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 20, 2022
@bradzacher
Copy link
Member

Unfortunately this is not really achievable because the types do not convey this information.
You cannot remove specific cases from the primitive types:

declare const x: string;
// typeof x === string

if (!x) { return };
// typeof x === string

if (x !== '') { return }
// typeof x === string

So in order for this to be a lint rule, we would need to invent some method for tracking this by tracking a type as it progresses through a function's control-flow. This is obviously not really possible for us to build.

Something like negated types in TS (microsoft/TypeScript#29317) would help solve this (because TS could refine the type to like string & not '', but without it - we're SOL.

@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels May 20, 2022
@Samuel-Therrien-Beslogic
Copy link
Contributor Author

Thank you for the explanation and reference. I went and upvoted it.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 20, 2022
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants