I have created simple react App during learning middleware redux-thunk. I have a problem with transpilation because of incorrect type of async action fetchPosts in interface PostListProps.
Below I show all code of the functional component where is the problem:
import React, { useEffect } from 'react'
import { connect } from 'react-redux'
import { fetchPosts } from '../actions'
import Action from '../actions/actionTypes'
import { Dispatch } from "react";
import State from '../reducers/State'
interface PostListProps {
fetchPosts: () => (dispatch: Dispatch<Action>) => Promise<void>
}
const PostList = (props: PostListProps) => {
useEffect(() => {
props.fetchPosts()
}, [])
return (
<div>Post List</div>
)
}
const mapStateToProps = (state: State) => {
return { posts: state.posts };
};
export default connect(
mapStateToProps,
{ fetchPosts }
)(PostList);
My code is transpiled without error only when I change type of fetchPosts function to:
fetchPosts: () => any
Below I present code of function fetchPosts:
import Action, { ActionType } from "./actionTypes";
import jsonPlaceholder from "../apis/jsonPlaceholder";
import { Dispatch } from "react";
import Post from '../apis/Post'
import State from "../reducers/State";
import { AxiosResponse } from "axios";
export const fetchPosts = () => {
return async (dispatch: Dispatch<Action>) => {
const response: AxiosResponse<Post[]> = await jsonPlaceholder.get<Post[]>('/posts')
dispatch({type: ActionType.FETCH_POSTS, payload: response.data });
}
}
I have no idea where can be a problem? Why my type of fetchPosts() is wrong? I would like to find out. I would be grateful for help.