Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Description
Before You File a Proposal Please Confirm You Have Done The Following...
- I havesearched for related issues and found none that match my proposal.
- I have searched thecurrent rule list and found no rules that match my proposal.
- I haveread 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
I've thought more about the error message change in#9643 /#10194, and I'm not happy with the current error message (changed in#10194).
'Unnecessary conditional, comparison is always {{trueOrFalse}}. Both sides of the comparison always have a literal type.' is more helpful than the prior message was, yes, but it doesn't fully explain what we're flagging to the user.
I propose we change it to 'Unnecessary conditional, comparison is always {{trueOrFalse}}, since{{left}} {{operator}} {{right}}
is {{trueOrFalse}}.', so that something likeif (expressionResultingInMinus55 < expressionResultingIn23) {}
will report an error as 'Unnecessary conditional, comparison is always true, since-55 < 23
is true.'.
This would IMO make it clearerwhat types we're seeing andwhy that indicates the expression has a constant value, helping the user assess whether the types are correct before resolving the complaint.
I have successfully implemented this in#10454 and I find the reports to be a big improvement when experimenting with it in the playground.
Fail
// unnecessary condition; 1 === 2 is falseif(1===2){}declareconstexpressionResultingIn23:23;declareconstexpressionResultingInMinus55:-55;// unnecessary condition; -55 < 23 is true.if(expressionResultingInMinus55<expressionResultingIn23){}
Pass
n/a