-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Nullish coalescing operator (??) #26578
Comments
This is new expression-level syntaxx. It's already a proposal for JS: https://github.com/tc39/proposal-nullish-coalescing . |
@andy-ms Shouldn't the tag be waiting for TC39, not out of scope? |
Nullish coalescing has reached stage 2. Can this issue be reopened or will this be addressed elsewhere? |
@MatthiasKunnen given the close connection, I'm guessing this will be brought in along with #16, but the general rule seems to be to wait for stage 3, so we have a bit longer to wait yet. |
Stage 3 now! |
^^ source |
You're welcome! |
Could i take a try on this? |
@rbuckton has an old branch that implements this (I would link you to it but it's searchable and I'm on mobile), so it depends on how bad the merge conflict would be and whether he would mind. |
@DanielRosenwasser Okay, never mind I found that branch and that is committed at |
Stage 3 now!!!! |
@DanielRosenwasser Can I try this? If @Kingwl is not still interested that is. |
@dragomirtitian 🤷🏻♂️ Seems @rbuckton has nearly completely implemented that(without tests) |
Is nullish coalescing also planned for TypeScript 3.7.0? |
@MatthiasKunnen Given that optional chaining (#16) is listed for 3.7.0, I presume these would land together (as the proposals go hand-in-hand). |
@jhpratt, I'm aware of optional chaining's milestone but don't want to assume nullish coalescing's milestone. That's why I asked |
Pardon my ignorance, but what about the inverse case? Does an an issue/proposal for that exist? Or is there a trick to achieve this now using existing operators? In my experience the inverse problem is perhaps even more common, where
Will not display a |
🤔 |
@RyanCavanaugh: tsx/jsx |
Hey folks (in particular @Kingwl and @dragomirtitian) - we'd love to have this feature shipped as a headlining "community-implemented" feature in 3.7. I think whoever claims dibs first can put up a draft PR and then hopefully work with others to contribute fixes, finish things out, and add testcases. @sheetalkamat will be helping coordinate and offer advice / code review throughout the process. Thanks! |
@RyanCavanaugh Hey, I have a 6 day chunk of time free time starting tomorrow, so I can start on this right away. @sheetalkamat Should I start from @rbuckton 's old branch? Or should I start from scratch? Thanks! |
@dragomirtitian |
@Kingwl Hi, sure, I'd be happy to collaborate on what is already there. Are you on gitter ? Maybe we can sync there as to what work still needs to be done and how to divide it up. |
@dragomirtitian Sure, Thanks |
thank you so much on the feature! |
You're welcome! If you would like to suggest improvements, feel free to open another issue. |
Config.active will always be true after that, if it was a Boolean before
…On Tue, Jun 2, 2020, 12:12 PM akopchinskiy ***@***.***> wrote:
@samterainsights <https://github.com/samterainsights>
config.name = config.name ?? "(no name)";
config.items = config.items ?? -1;
config.active = config.active ?? true;
Why not to use?
config.name = config.name || "(no name)";
config.items = config.items || -1;
config.active = config.active || true;
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#26578 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADHFRL362X5K7NB7SFRQ2VLRUUQGVANCNFSM4FQX6DIA>
.
|
Search Terms
Suggestion
A nullish coalescing operator, which is essentially an alternative to
||
that will only return the right-side expression if the left-side isnull
orundefined
(||
returns the right-side if the left-side is falsy, i.e.null | undefined | "" | 0 | false | NaN
).Use Cases
This would be extremely useful anywhere where defaults are needed for potentially omitted inputs, but empty strings or
0
, and so forth, are valid inputs that shouldn't be defaulted. My particular use case is AngularJS component bindings. Whenever bindings are updated, I would like to provide a default for a string or boolean binding only when they are straight up omitted (undefined
), but I have to do a full-fledged ternary expression withtypeof
to handle the falsy input corner-case.In conjunction with the safe chaining operator (#16), handling optionals would be trivial.
Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: