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 type error when only passing in only mapStateToProps on flow v0.55.0

See original GitHub issue

Previously on flow v0.54.1, the following code checked out fine

// @flow
import { connect, type Connector } from 'react-redux'

type State = {
  val: string,
}
type OwnProps = {
  other: string,
}
type Props = {
  other: string,
  val: string,
}

const mapStateToProps = (state: State) => {
  return {
    val: state.val,
  }
}

const connector: Connector<OwnProps, Props> = connect(mapStateToProps)

After upgrading flow to v0.55.0, flow gives an error

Error: src/components/body/custom-events/department-transfer-container.js:29
 29: const connector: Connector<OwnProps, Props> = connect(mapStateToProps)
                                                   ^^^^^^^^^^^^^^^^^^^^^^^^ function call. Could not decide which case to select
 29: const connector: Connector<OwnProps, Props> = connect(mapStateToProps)
                                                   ^^^^^^^ intersection type
  Case 3 may work:
                                 v--------------
   78:   declare function connect<S, A, OP, SP>(
   79:     mapStateToProps: MapStateToProps<S, OP, SP>,
   80:     mapDispatchToProps: Null,
  ...:
   83:   ): Connector<OP, $Supertype<SP & { dispatch: Dispatch<A> } & OP>>
         ----------------------------------------------------------------^ polymorphic type: function type. See lib: flow-typed/npm/react-redux_v5.x.x.js:78
  But if it doesn't, case 5 looks promising too:
                                 v------------------
   92:   declare function connect<S, A, OP, SP, DP>(
   93:     mapStateToProps: MapStateToProps<S, OP, SP>,
   94:     mapDispatchToProps: MapDispatchToProps<A, OP, DP>,
  ...:
   97:   ): Connector<OP, $Supertype<SP & DP & OP>>
         -----------------------------------------^ polymorphic type: function type. See lib: flow-typed/npm/react-redux_v5.x.x.js:92
  Please provide additional annotation(s) to determine whether case 3 works (or consider merging it with case 5):
   14: const mapStateToProps = (state: Object, ownProps: OwnProps) => {
                                                                     ^ return

I can resolve the error by passing in an empty mapDispatchToProps like so

const mapDispatchToProps = (dispatch: Dispatch<*>) => {
  return {}
}
const connector: Connector<OwnProps, Props> = connect(
  mapStateToProps,
  mapDispatchToProps
)

but hoping there might be a way to adjust the libdef to support not passing in mapDispatchToProps like before.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:37
  • Comments:23 (14 by maintainers)

github_iconTop GitHub Comments

31reactions
sportocommented, Sep 26, 2017

Annotating mapStateToProps has worked for me.

import type { MapStateToProps } from "react-redux"

const mapStateToProps: MapStateToProps<*, *, *> = (state: StateType) => {
  return ...
}

export default connect(mapStateToProps)(...)

Flow infers what version of connect to use this way.

4reactions
adamesquecommented, Oct 18, 2017

We were wrestling with this issue, and it turned out the problem was that we weren’t declaring a return type for our mapStateToProps function. Adding a signature like

function mapStateToProps(state: State): StateProps {
    return …;
}

resolved the type error. It was just an oversight on our part.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flow throwing type errors for using Redux connect
I believe you'll need to add a type annotation to your dispatch . Example: function mapDispatchToProps(dispatch: Dispatch) { return ...
Read more >
Redux & flow-typed で mapStateToProps に型を付ける
export default connect(mapStateToProps)(...) 参考: react-redux type error when only passing in only mapStateToProps on flow v0.55.0 #1269.
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
As the first argument passed in to connect , mapStateToProps is used for selecting the part of the data from the store that...
Read more >
Annotating Connected Components with Flow
First, you need to explicitly annotate each connected components at file exports. This shall clear all the “implicitly instantiated” errors.
Read more >
Adventures in Static Typing: React, Redux, Flow, Oh My
Inexact props are not safe. Flow has a concept of exact object types in which objects are only allowed to have an exact...
Read more >

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