Typing issues with react-redux connect and class components
See original GitHub issueNot 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:
- Created 5 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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:
@bschlenk Could you please check if the above trick fixes your case?
@supermihi it works fine, the only thing you should do, is just define the type of component props clearly. you can refer to this: