Problem: In TypeScript, developers often declare needless conditionals
Problem: In TypeScript, developers often declare needless conditionals.
Why needless conditionals are a problem:
🚩 Adds noise
🚩 Hurts readability
🚩 Creates confusion
🚩 Often a sign of a misunderstanding or a logic error
Solution: typescript-eslint/no-unnecessary-condition
I just used this rule to find and automatically fix 100’s of bad conditionals in a large codebase.
Here are 4 examples of bad conditionals it found:
1. The author didn't know that map always returns an array. So the fallback to an array was needless.
2. The condition is already checked on 123, so the check on 129 was redundant.
3. Here it caught a logic error! The "??" was reported as needless. The developer meant to check the array's length.
4. The variable is always defined because it's narrowed on line 90. So, the "?." on 92-95 were all needless.
I'm so impressed with this rule! I plan to run it on all TypeScript projects in the future.




Here's another error it found - The developer forgot to invoke the function.
Seriously, this lint rule is 🔥.

Here it reported the final "if" on line 36 as needless because "box.type" only has 3 potential values. (So the first two checks made the last "if" condition needless).
The solution? Remove the last "if", or better yet, use a switch or map instead to clarify intent.
