I'm using typescript in an application, but found some problem with react-redux. The 'connect' method reports a problem and I have no idea with it since I'm a rookie with typescript and redux. What should I do or where in my code should be modified? Thanks a lot
Application built with [email protected]
, [email protected]
, [email protected]
.
// article
interface IArticle {
title: string,
author: string
}
// state
interface MyState {
list: [],
article: IArticle
}
// stateMapToProps
interface MyStateProps {
article: IArticle
}
// router match
interface MatchParams {
id: string
}
// own props
interface MyOwnProps extends RouteComponentProps < MatchParams > {
article: IArticle,
dispatch: (o: object) => {}
}
class ArticleContainer extends Component < MyOwnProps, {} > {
constructor(props: MyOwnProps) {
super(props);
}
componentDidMount() {
const {
dispatch
} = this.props;
const id = this.props.match.params.id
dispatch(fetchArticle(id))
}
render() {
const {
article
} = this.props;
return ( <
Article article = {
article
} > < /Article>
)
}
}
const mapStateToProps = (state: MyState): MyStateProps => {
return {
article: state.article
}
}
export default connect < MyStateProps, {}, {
article: IArticle
} > (
mapStateToProps
)(ArticleContainer)
Here is the code of async action fetchArticle
function fetchArticle(id: string) {
return function(dispatch: (action: AnyAction) => {}): Promise<void> {
dispatch(getArticle(id))
return axios.get(`/article/${id}`)
.then(res => {
dispatch(getArticleSuccess(res.data))
})
}
}
Error happens at the export
line and message is as below:
Argument of type '(state: MyState) => MyStateProps' is not assignable to parameter of type 'MapStateToPropsParam'. Type '(state: MyState) => MyStateProps' is not assignable to type 'MapStateToPropsFactory'. Types of parameters 'state' and 'initialState' are incompatible. Type '{}' is missing the following properties from type 'MyState': list, articlets(2345)
export default connect(mapStateToProps)(ArticleContainer);
– eenagyComponent<MyOwnProps, MyState>
– eenagyArticleContainer
part inexport
line report a message, also theinterface MyState
represents the structure of the store created with redux, not the 'state' of theClass
, so I think there is no need to put it in the generics. I hope I make myself understandable. – smilebuzArticleContainer
inconnect
part is as below(only record the first sentence since it is too long to write here): Argument of type 'typeof ArticleContainer' is not assignable to parameter of type 'ComponentType<Matching<MyStateProps & DispatchProp<AnyAction>, MyOwnProps>>'......ts(2345) – smilebuz