-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checking for void in logical operations #7256
Comments
It's certainly very suspicious. We can look at how much code this might break. |
Disallowing
Running tests now. |
Oh wow, that would save me from the world of problems! Please let me know whether this will make it into "next" version. I guess this can be configurable with a parameter in tsconfig. |
can we instead add a flag that prohibits non-boolean expressions where a boolean is expected? #7306 |
@DanielRosenwasser would you be able to share with us, what is the result? Will this (flag) make it to the compiler? Thanks! |
This would be pretty great to have in the compiler. Linking to our TSLint issue tracking this: palantir/tslint#253 |
@tomitrescak sorry, I've been sidetracked on other things. The team will need to review whether we're certain that it's a net positive as well. In the mean time, my pull request can be found at #7261. I don't think there's a ton of controversial stuff in it. Feel free to try it out, review it, and get back to me. |
👍 great idea |
sometimes I really hard to follow your reasoning void method/functions return undefined, returning undefined is a pretty normal thing in idiomatic JavaScript because it is one of the few types coerces to a false value naturally, so it seems an overkill to ban it on a this can never happen premise a more vicious problem is when you had a boolean value in a condition expression, but after refactoring it turned, say, into a lazy boolean can we flag it? oh sure, tslint (well actually something more capable that that) to the rescue, I know, I know |
I don't understand this logic. The point is that a conditional on a known-falsy value is almost certainly a bug because it creates either unreachable code, or code that appears conditionally-executed when it is in fact unconditionally executed. On the flipside, the problem is that there's lots of idiomatic code that does stuff like this: // x.filterFunc: () => boolean
if (x.filterFunc) {
arr = arr.filter(x.filterFunc);
} Certainly one can have the opinion that it would be more correct to write |
what about void returning overloads mixed with non voids ones? speaking of style, it's not sbout style at all, the problem is (seen it a before var isFirst:boolean; after refactoring var isFirst : () => boolean; On Mar 14, 2016 10:59 PM, "Ryan Cavanaugh" notifications@github.com wrote:
|
I totally understand how you could find yourself in that state. I've been there myself. At the same time, there is a lot of code that is in that state on purpose that does not have any bugs. |
I'll note that it's also very frequent that people (like me) use numbers in conditionals. It's not that uncommon for me to do something like this: if (list.length) {
// list isn't empty, let's do something with it.
} So I personally agree that it's best left to a linter. |
Done. Tracking break at #26262 |
In many cases it is not desirable to allow comparison with undefined. Is it somehow enforceable that this will be detected during compilation?
I guess this is by design not available.
The text was updated successfully, but these errors were encountered: