4 ms·
Plain vanilla JS (and therefore TypeScript) still lacks one killer feature from the Coffee family: Null propagation operators (`a ? b` => `a != null ? a : b`, `
by smilekzs 9y ago
Plain vanilla JS (and therefore TypeScript) still lacks one killer feature from the Coffee family: Null propagation operators (`a ? b` => `a != null ? a : b`, `a?.x` => `a == null ? null : a.x`, etc). Even C# has it.
- sudowork 9y agoI agree, it's one of the features we'll miss. There is a Stage 1 Null Propagation proposal: https://github.com/tc39/proposal-optional-chaining https://github.com/tc39/proposal-optional-chaining
- evan_ 9y agoThere's a babel transform for this in case you want to start using it now, before browsers support it: https://github.com/babel/babel/tree/7.0/packages/babel-plugin-transform-optional-chaining https://github.com/babel/babel/tree/7.0/packages/babel-plugi...
- sudowork 9y agoThanks for the tip! Given some of the lessons we've learned with CoffeeScript, we'll most likely wait until proposals hit at minimum stage 3 before adopting them.
- btown 9y agoI've been waiting for this for years. Even considered forking Babel... it's been a long time coming :)
- smilekzs 9y agoI want to use TypeScript too, which will probably not adopt it until standardization.
- DanRosenwasser 9y agoWe definitely want to implement it! But many parts of that specification are still in flux, so like you said, we'd like to see the proposal stabilize first to avoid problems.
- abritinthebay 9y agoGiven null is generally non-idiomatic in JS (it's there, but most best practices say to avoid it) I think that's likely by design. However the `a?.b` form is coming (it's not just null propagation, it's an Existence Operator). It's called "optional chaining" https://github.com/tc39/proposal-optional-chaining https://github.com/tc39/proposal-optional-chaining
- smilekzs 9y ago> I think that's likely by desig Fine. s/null/undefined The problem still exists.
- abritinthebay 9y agoI think calling it a problem is a bit melodramatic. A cleaner syntax is nice for sure, but it’s not exactly unworkable as is (and promotes flatter data, generally a good thing). But yes, it’ll be nice if/when the new syntax drops.
- Jakob 9y agoThe first one kind of works with `a || b`. Returns b if a is falsey (null, undefined, 0, "").
- smilekzs 9y ago> kind of works !== works
- alanh 9y agoThe other killer feature that is still missing is multi-line regex with inline comments. I know, you shouldn't be writing big regexes if possible… but when you need to, it's so nice to be able to actually explain what the heck you are doing!