-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Remove the code style rule about casts to bool #26220
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, reading this, I actually agree with it. If I am storing a boolean value in a variable I do want to be explicit about it, and probably also if I'm using
&&
or||
in an if condition.But I would like to allow
if (myObject) {
at least for things that areMyClass | undefined
orMyClass | null
since I think it is clearer and easily understood.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also find myself agreeing with the rule at least as given by the example here. Perhaps we just need to make this clearer & more specific, both in the explanation and with more examples? If anything, the problem with the example above is not that there is an implicit cast, it is that there no cast at all. That is to say,
isRealUser
will be a string object in memory (assuming that's what userId etc. is).Maybe something like:
If a variable's type should be boolean, make sure it really is one.
I've left out function arguments here: passing a non-boolean as a boolean would cause a type error (at least in the TS playground).
I don't think this is enforceable with eslint, sadly, but hopefully with a stricter definition we might stand a better chance of enforcing it socially?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully agree with @dbkr 's comment, and I think we somewhat already follow this, implying that @richvdh 's concern about how to enforce it if we can't write a rule might possibly not apply here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think I'd find @dbkr 's proposal acceptable.
good provided what comes after the
&&
is also a boolean, of course. Might also be worth including an example with||
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mm, good point. I'll paste an edited version rather than editing the version above, hopefully that's least confusing:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dbkr do you fancy opening a new PR for that 😇 ? We could use this one but tbh it's a long way from the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay - done: #26317