question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-redux-5.x.x][flow_v0.89.x-] Unexpected behaviour

See original GitHub issue

This code:

// @flow

import React from 'react';
import { connect } from 'react-redux';

// import type { Location } from 'react-router-dom';

type Action1 = {| type: string |};
type Dispatch1 = Action1 => Action1;

const action1 = (): Action1 => ({ type: 'str' });

type DispatchProps = {| action1: () => Action1 |};
type MapDispatchToPropsFn = Dispatch1 => DispatchProps;

const mapDispatchToProps: MapDispatchToPropsFn = (dispatch: Dispatch1) => ({
  action1: (...args) => dispatch(action1(...args)),
});

type OwnProps = {|
  location: Location,
|};

type Props = { ...OwnProps, ...DispatchProps };

const Com = (props: Props) => props.toString();

export default connect<Props, OwnProps, _, DispatchProps, _, Dispatch1>(
  null,
  mapDispatchToProps,
)(Com);

Passes flow checks, but if uncomment the line // import type { Location } from 'react-router-dom';:

     src/Test.jsx
     26│ const Com = (props: Props) => props.toString();
     27│
     28│ export default connect<Props, OwnProps, _, DispatchProps, _, Dispatch1>(
 [1] 29│   null,
     30│   mapDispatchToProps,
     31│ )(Com);
     32│

     flow-typed/npm/react-redux_v5.x.x.js
 [2] 50│     | ((state: S, ownProps: OP) => SP)
       :
 [3] 56│     | ((state: S, ownProps: OP) => (state: S, ownProps: OP) => SP);

Any imported from react-router-dom type (ContextRouter, Match, Location, etc.) and used in OwnParams causes the error.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rkurbatovcommented, Mar 17, 2019

@paravozz no-no! I re-exported it in the project. Like:

// modules/types/site.js

import { ContextRouter } from 'react-router'

export type FixMeContextRouter = ContextRouter

and then

import { FixMeContextRouter } from 'types/site'

That was the only unresolved problem in our project. Now I have 4000 lines of code PR doing literally nothing than new flow 😃

0reactions
AndrewSouthpawcommented, Mar 25, 2019

This is how we’ve done it in our own codebase, I’m manually tricking Flow in the MSTP.

type OwnPropsT = {||}

type PropsT = {|
  ...OwnPropsT,
  navigation: NavigationT<*>,
|}

export class _Foo extends React.Component<PropsT> { /* ... */ }

const mapStateToProps = (state: GlobalStateT, ownProps: OwnPropsT) => ({
  navigation: (ownProps: any).navigation, // trick Flow to handle other injected props
})

export const Foo = withNavigation(connect<PropsT, OwnPropsT, _, _, _, _>(mapStateToProps)(_Foo))

Doing it this way keeps the correct typing defined in PropsT.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found