Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Feb 28, 2018

Fixes #22230
Would make #22088 simpler

@ghost ghost requested review from sandersn and weswigham February 28, 2018 16:44
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good and I think the usages this enables are fine too. I'd like another reviewer to sign off before merging, just so we get two people thinking about unintended consequences.

@weswigham
Copy link
Member

I like this change, ofc. @rbuckton you're a real fan of the undefined-is-implicitly-bitwise-zero pattern; you agree with this?

@RyanCavanaugh
Copy link
Member

TIL

I'm hesitant on this because (a) you could just use ! and (b) this reasoning applies equally to e.g. "foo" | 0 (which is 0). If the logic is "It doesn't return NaN" then [3] | { } should be legal too, because the binary bitwise operators always return an int. I'm strawmanning of course, but what about e.g. string | number as an operand? Probably most codebases with a flag value would treat null/undefined as 0 but I'm not convinced that it's all of them.

@weswigham
Copy link
Member

Ofc, the reason we want it is because we use the fact that null/undefined are effectively zero in bitwise ops extensively inside our codebase. Tbqh, I think the question to ask, is how often does forcing you to handle null/undefined explicitly here find an error, versus just forcing you to write (and slowly become blind to) nonnull assertions.

@ghost
Copy link
Author

ghost commented Feb 28, 2018

Note: "1" | 2 is 3, as is ({ toString() { return "1" } }) | 2, so not a good idea to rely on anything else being treated as 0.

@typescript-bot
Copy link
Collaborator

Thanks for your contribution. This PR has not been updated in a while and cannot be automatically merged at the time being. For housekeeping purposes we are closing stale PRs. If you'd still like to continue working on this PR, please leave a message and one of the maintainers can reopen it.

@ghost ghost deleted the null_or_number branch July 2, 2018 19:07
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants