About type conversion
This article has been about type conversion
At my company, we had a situation where we had to format our code because eslint gave us some strong conditions. Among them, I encountered the sentence āRedundant double negation.eslintno-extra-boolean-castā the most.
Letās see what that error is, why it pops up, and what the solution is.
What is the āRedundant double negation.eslintno-extra-boolean-castā error?
When using eslint, the āRedundant double negation.eslintno-extra-boolean-castā error that you may see is often caused by the error is known as the āno-extra-boolean-castā rule. This rule detects unnecessary double negation operations and displays a warning.
if (!!value) {
// ...
}
This is what you see in code like this. I knew what it was.
Why does it come up?
As you can see in the example above, ā!!valueā forces a negative operation on the boolean value āvalueā, then repeats it twice by performing the negative operation again. While this is used as a form of casts and makes sense to us, it is unnecessary for the formatting recommended by eslint, and we believe it makes the code more complex and less readable.
Now that we know why weāre seeing that, letās see how we can fix it.
Solution
Since the above phrase is unnecessary as described, ESLINT recommends removing it. It should look like this
if (value) {
// ...
}
The modified code above uses āvalueā as it is to evaluate the condition. This eliminates unnecessary negative operations and makes the code more concise.
Reference
When using eslint to statically analyze code, the āRedundant double negationā rule is applied to detect similar unnecessary negative operations. By applying this rule, you can make your code more readable and avoid potential bugs.