Implement optional chaining and nullish coalescing
See original GitHub issuea?.b
→ a && a.b
Ok or fork?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Optional chaining and nullish coalescing in TypeScript
Optional chaining is often used together with nullish coalescing, which is the ability to fall back to a default value when the primary ......
Read more >What is Nullish Coalescing and Optional Chaining in ...
Nullish Coalescing is a way in which you use the Nullish Coalescing Operator in JavaScript to determine whether a value is nullish or...
Read more >Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The nullish coalescing operator may be used after optional chaining in order to build a default value when none was found:.
Read more >How can I use optional chaining and nullish coalescing in my ...
The optional chaining operator ( ?. ) allows us to access deeply nested object properties without having to validate each reference in the ......
Read more >Optional Chaining and Nullish Coalescing in TypeScript
Optional chaining allows you to write code that will immediately stop running expressions if it hits a null or undefined . ... A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yep, agreed! I’ve been thinking about this more over the last few days since the TS release, and I’ll reopen this issue and repurpose it to built-in support in Sucrase. I’ll add some more detail when I get a chance.
Chrome Canary now has optional chaining available by default, and in Chrome Beta it’s available behind the “Experimental JavaScript” flag, so it’s already reasonable to use it in development with Sucrase (which will emit it unchanged) for running code in the browser. My team runs tests in Node, though, and it looks like Node 14 (to be released next year) will be the first version with support. We also lag behind the latest Node, so it will be a while before we have native support for the syntax in tests. It seems like a high-enough value syntax that it can be an exception to the “Sucrase is hesitant to implement upcoming JS features” rule.
Sucrase transforms require a lot more thought and care than Babel transforms since Sucrase makes the parser as minimal as possible and transforms code in a left-to-right scan, so it’s tricky, but I think there will be a way to make it work.
I worked through the details and different options by writing up a technical plan at https://github.com/alangpierce/sucrase/wiki/Sucrase-Optional-Chaining-and-Nullish-Coalescing-Technical-Plan . I’ll get started on the implementation soon.