Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created November 10, 2017 16:23
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sibelius/32ea5d07fa3bcb44edb8aa47d185c4ca to your computer and use it in GitHub Desktop.
Save sibelius/32ea5d07fa3bcb44edb8aa47d185c4ca to your computer and use it in GitHub Desktop.
How to easily type Redux Props with Flow

How to easily type Redux Props with Flow

Add ExtractReturn helper

// https://hackernoon.com/redux-flow-type-getting-the-maximum-benefit-from-the-fewest-key-strokes-5c006c54ec87
// https://github.com/facebook/flow/issues/4002
// eslint-disable-next-line no-unused-vars
type _ExtractReturn<B, F: (...args: any[]) => B> = B;
export type ExtractReturn<F> = _ExtractReturn<*, F>;

Extract return of mapStateToProps and mapDispatchToProps

const mapStateToProps = state => ({
  user: state.user,
});
const mapDispatchToProps = dispatch => ({
  actions: {
    logout: () => dispatch(logout()),
  },
});

type ReduxProps = ExtractReturn<typeof mapStateToProps>;
type ReduxActions = ExtractReturn<typeof mapDispatchToProps>;

Add ReduxProps and ReduxActions to your Component props

type Props = {} & ReduxProps & ReduxState

class EntriaComp extends Component<Props> {}
@Andarist
Copy link

Just pasting in @calebmer's version of the ExtractReturn:

ExtractReturn is even better with $Call:
$Call<((...Iterable) => T) => T, Fn>

I want to go back to that later and have both versions in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment