Skip to content

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

Closed
@kirkwaiblinger

Description

@kirkwaiblinger

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 no-unnecessary-condition 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');
  }
}

assert(true)

Pass

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

assert(Math.random() > 0.5)

Additional Info

No response

Metadata

Metadata

Labels

enhancement: 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-pluginteam assignedA member of the typescript-eslint team should work on this.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions