Flow: Flow can't disambiguate between two sides of an union when they're functions to different types

Created on 26 Apr 2018  路  6Comments  路  Source: facebook/flow

Given an Union like so:

type Fn = () => number
type FnFn = () => Fn
type Union = Fn | FnFn

flow won't be able to disambiguate between the two options, even though it's clear that a function returning a function is different from a function returning a number.

Reproducible with:
https://flow.org/try/#0C4TwDgpgBAYgdlAvFAFASiQPinArgWwCMIAnAKFEljniVQ0W3gvGgFU4BLAewWVoA+1ZmQDGvAM7AoAMwCMALmp10WKHLGTpMgExL4tZKsb01GzXCmyAzEo48+pkxvGXtAFjtdeKhtmPYckA

bug feature request

All 6 comments

This support is required to be able to type react redux - see the linked issue, so it would be awesome if someone could take a look or point us in the right direction...

Well, there's an easy workaround

Yes we have that work-around If you see the flow types issue, but flow typed don鈥檛 want to require this typing because they consider it a flow bug.

If you tell us it can鈥檛 be done and we need to be explicit we can take that back to flow typed... but it would be nice to fix it.

@apsavin The workaround introduces a huge amount of boilerplate when used in the content of react-redux and any connected components. The current flow-typed libdef for react-redux makes it possible to create a mapStateToProps function, pass it to connect, and flow understands all and is happy. However, the connect function also takes a factory version of mapStateToProps, which actually becomes necessary for any sufficiently ambitious react redux app when it comes time to improving app performance, and because flow cannot disambiguate a function that returns an object (regular mapStateToProps) from a function that returns a function that returns an object (the mapState factory), supporting both in the libdef would require annotating every mapStateToProps, which is understandably not something the flow-typed maintainers are eager to adopt.

Agree.

@mrkev Are there any chances that this issue will be addressed soon?

@apsavin wish I knew, this would be very useful to me too.

I'm not in the core flow team, I help with PRs here when I get a chance, and the last few months work has kept me busy and out of touch 馃槄

Was this page helpful?
0 / 5 - 0 ratings