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.

Typing issues with react-redux connect and class components

See original GitHub issue

Not sure if this is related to #51 or #32, but I have issues with typescript-fsa in connection to react-redux’ connect and class components.

In the following example, the class component version does not compile, but the functional component version does:

import * as React from 'react';
import actionCreatorFactory, { ActionCreator } from 'typescript-fsa';
import { connect } from 'react-redux';

interface Props {
  doSomething: (thing: string) => void;
}
class MyComponent extends React.Component<Props> {
  render() {
    return <button onClick={() => this.props.doSomething('test')}/>;
  }
}
const MyFunctionalComponent = (props: Props) => <button onClick={() => props.doSomething('test')}/>;

const createActionCreator = actionCreatorFactory();
const doSomething = createActionCreator<string>('DO_SOMETHING');

const mapDispatchToProps = {
  doSomething,
};
connect(null, mapDispatchToProps)(MyComponent); // error: Argument not assignable
connect(null, mapDispatchToProps)(MyFunctionalComponent); // works

Both work if I declare Props as

interface Props {
  doSomething: ActionCreator<string>;
}

But I obviously do not want to couple the component to redux-specific stuff. Using typescript 2.8 and the most up-to-date version of all packages. Any ideas?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
aikovencommented, May 31, 2018

@supermihi I’m sorry for not answering for so long.

I took a closer look and it still seems that this has to do with react-redux typings. They do a lot of work to give you better type inference, but sometimes it confuses the compiler.

I managed to fix the error you get by specifying type arguments explicitly:

connect<{}, Props>(null, mapDispatchToProps)(MyComponent); // no error

@bschlenk Could you please check if the above trick fixes your case?

0reactions
lucas1677commented, Jun 7, 2018

@supermihi it works fine, the only thing you should do, is just define the type of component props clearly. you can refer to this:

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

import {TodoAppState, TodoItemState} from '@src/types/todoApp';

const TodoItem = ({id, isComplete, name}) => (
  <li>
    <input type="checkbox" defaultChecked={isComplete}/>
    {name}
  </li>
);

type PropsType = {
  todos: TodoItemState[];
};

class TodoList extends React.Component<PropsType> {
  render() {
    return (
      <div className="Todo-list">
        <ul>
          {this.props.todos.map(todo => <TodoItem key={todo.id} {...todo}/>)}
        </ul>
      </div>
    );
  }
}

export default connect(
  (state: TodoAppState) => ({
    todos: state.todos,
  })
)(TodoList);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using React Redux Connect with TypeScript
When using Redux in a TypeScript React Application, we will run into some snags. One of the main ones is correctly typing Components...
Read more >
Typescript throwing error on basic class component in usage ...
All I had to do was: export default connect(mapStateToProps)(Filter);.
Read more >
React Redux connect(): When and how to use it
The React Redux connect() API is used for creating container elements that are connected to the Redux store. The Redux store is derived...
Read more >
Usage with TypeScript - React Redux
The connect higher-order component is somewhat complex to type, because there are 3 sources of props: mapStateToProps , mapDispatchToProps , and ...
Read more >
Typescript Tips Series: Proper Typing of react-redux ... - Medium
... defining the typescript types for a react-redux connected component. ... type Props = StateProps & DispatchProps & OwnProps}export class ...
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