flow icon indicating copy to clipboard operation
flow copied to clipboard

Support optional chaining

Open vjpr opened this issue 9 years ago • 55 comments

When could we expect optional chaining support.

It will be released soon with babel@7.

https://github.com/babel/babylon/pull/545

vjpr avatar Jul 03 '17 14:07 vjpr

this is super important to make the code more readable. i'd love to have this.

7ynk3r avatar Sep 19 '17 20:09 7ynk3r

this feature is really important for project i am working for. waiting for implementation!

eshikerya avatar Oct 21 '17 12:10 eshikerya

This is really important for dealing with JSON responses without having to write a plethora of null checks. +1

suparngp avatar Oct 21 '17 18:10 suparngp

Note that optional chaining is still a stage 1 proposal and has gone through a number of changes since the Babel plugin was implemented. It's very likely that there will be changes to both the syntax and the behavior before the final JS feature is released.

See various other discussions: https://github.com/tc39/proposal-optional-chaining https://github.com/rwaldron/tc39-notes https://github.com/Microsoft/TypeScript/issues/16

alangpierce avatar Oct 21 '17 22:10 alangpierce

Can be a simple case of optional chaining (optional static property access) implemented today under option's flag esproposal or experemental?

I say about simple property reading from nested objects a?.b?.c, which will be an amazing improvement in the code for React components. Eg when reading graphql responses.

And leave dynamic and function/method calls uncovered while it in stage-1.

cc @calebmer @mroch

nodkz avatar Jan 03 '18 04:01 nodkz

which will be an amazing improvement in the code for React components. Eg when reading graphql responses.

would love to see that! we could remove tons of code and checks if flow also supports this syntax. thanks @nodkz and @alangpierce for having an eye on that.

zanettin avatar Jan 17 '18 16:01 zanettin

looks like this will be in the next version 🎉https://github.com/facebook/flow/commit/763ffb509b237e2358bbace871732ee3e2bd623b

cloudkite avatar Mar 05 '18 07:03 cloudkite

@cloudkite looks like only parsing will be supported. Type support isn't landing, as the feature has been marked as experimental.

niieani avatar Mar 05 '18 12:03 niieani

I've added (most) type support for optional chaining in 0.71.0 (gated behind the esproposal.optional_chaining flag). The exception is that you'll get a Flow error if you try to call a member expression where the call and/or member is part of an optional chain. Once I get optional chaining to play nice with our implementation of methods, I'll remove this restriction.

fishythefish avatar Apr 27 '18 21:04 fishythefish

This is amazing, I've been waiting for this.

In the following situation it complains about dash.exampleLogs[x] on line 319 because dash.exampleLogs might be undefined, however given that this code will only run if line 318 is true, shouldn't Flow realize that this cannot be undefined in this context?

Cannot get dash.exampleLogs[x] because an indexer property is missing in undefined [1].

     frog/imports/ui/Preview/ShowDashExample.js
     316│               const dash = aTdashs[this.state.example];
     317│               let data;
     318│               if (dash?.exampleLogs?.[x].type === 'state') {
     319│                 data = dash.exampleLogs[x].state;
     320│               } else if (dash.prepareDataForDisplay) {
     321│                 data = dash.prepareDataForDisplay(
     322│                   dash.initData,

     frog-utils/dist/types.js.flow
 [1] 157│     | {
     158│         type: 'logs',
     159│         title: string,
     160│         path: string,
     161│         activityMerge?: Object,
     162│         instances?: number
     163│       }
     164│     | { title: string, type: 'state', activityMerge?: Object, state: any }
     165│   )[]

houshuang avatar May 06 '18 00:05 houshuang

@houshuang We could potentially do that, but Flow doesn't currently perform that refinement.

fishythefish avatar May 07 '18 20:05 fishythefish

@houshuang feel free to create a separate issue to track and discuss that

mrkev avatar May 07 '18 21:05 mrkev

@houshuang @fishythefish @mrkev was a separate issue ever created? I too am finding it hard to make use of optional chaining when flow complains that I haven't checked for null (when I have).

murrayju avatar Jul 19 '18 05:07 murrayju

I haven't seen one.

mrkev avatar Jul 19 '18 09:07 mrkev

https://github.com/facebook/flow/issues/6613

Hypnosphi avatar Jul 19 '18 13:07 Hypnosphi

Babel 7 RC has been released - it would be nice to have this and null coalescing ready when Babel 7 is fully released

0rvar avatar Aug 13 '18 13:08 0rvar

Babel 7 is being released this week: https://twitter.com/left_pad/status/1031700991186489344

0rvar avatar Aug 22 '18 11:08 0rvar

Any progress on this? Babel 7 is now fully released: https://babeljs.io/blog/2018/08/27/7.0.0

bsmith-cycorp avatar Aug 29 '18 19:08 bsmith-cycorp

I can only speculate as I'm not a core contributor to Flow but Just because babel supports a feature, doesn't mean Flow will or wont support it. optional chaining happens to be partially supported in Flow already but function calls are not yet supported. Maybe someone from the Flow core team could comment on if adding full support is planned? Or is this a feature the core team would be willing to merge but not prioritized?

vicapow avatar Aug 29 '18 19:08 vicapow

function calls not supported and typing also. the optional property become ?any type(afaik)

eshikerya avatar Aug 29 '18 19:08 eshikerya