Closed
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/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