Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Enhancement: [no-unnecessary-condition] Check truthiness assertion functions #9076

Open
4 tasks done
kirkwaiblinger opened this issue May 11, 2024 · 0 comments
Open
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@kirkwaiblinger
Copy link
Member

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

This is spinoff from the corresponding issue for strict-boolean-expressions.

Following the conversation from #9009 (comment), it is proposed that strict-boolean-expressions also check the asserting argument of an assertion function as a boolean context for an unnecessary condition.

However, for this rule, that behavior should definitely be behind an option, since assertion functions can often be used to assert that something which appears to be true according to the types actually is true at runtime. Codebases that do so regularly will probably want to disable this behavior.

Fail

function assert(x: unknown): asserts x {
  if (!x) {
    throw new Error('assertion');
  }
}

const truthyString = 'string';
assert(truthyString)

Pass

function assert(x: unknown): asserts x {
  if (!x) {
    throw new Error('assertion');
  }
}

assert(Math.random() > 0.5)

Additional Info

No response

@kirkwaiblinger kirkwaiblinger added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look enhancement: plugin rule option New rule option for an existing eslint-plugin rule labels May 11, 2024
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants