gpt4 book ai didi

reactjs - mapDispatchToProps 上的 Redux Thunk Typescript 错误

转载 作者:行者123 更新时间:2023-12-03 21:21:35 26 4
gpt4 key购买 nike

尝试创建我的 mapDispatchToProps 函数时,我的容器组件遇到 typescript 错误,因为我的 Thunk 函数没有返回包含属性“Type”的对象。我的 Thunk 返回一个 Promise,它本身没有 'Type' 属性,但会调度一个里面有 'Type' 的 Action 。我不知道如何告诉 typescript 这没问题。

我得到的错误是

Argument of type '(dispatch: Dispatch<ActionTypes>, getState: () => IStoreState) => Promise<void>' is not assignable to parameter of type 'ActionTypes'.
Property 'type' is missing in type '(dispatch: Dispatch<ActionTypes>, getState: () => IStoreState) => Promise<void>'.

Action 类型:
export interface IFetchJokeSuccessAction {
readonly type: ActionTypeKeys.FETCH_JOKE_SUCCESS;
readonly payload: string;
}

export interface IFetchJokeInProgressAction {
payload: string;
readonly type: ActionTypeKeys.FETCH_JOKE_INPROGRESS
}

export interface IFetchJokeFailAction {
readonly type: ActionTypeKeys.FETCH_JOKE_FAIL;
readonly payload: string;
}

export interface IClearJokeAction {
readonly type: ActionTypeKeys.CLEAR_JOKE
}

type ActionTypes = IFetchJokeSuccessAction | IFetchJokeInProgressAction | IFetchJokeFailAction | IClearJokeAction;

这是我对组件的调度:
interface IDispatchProps {
clearJoke: () => any;
fetchJoke: () => any;
}
const mapDispatchToProps = (dispatch: Dispatch<ActionTypes>): IDispatchProps => {
return {
clearJoke: () => dispatch(clearJoke()), // No problem, this is a regular action
fetchJoke: () => dispatch(fetchJoke()) // Problem, this is a Thunk
}
};

以下是我的行动:
import { Dispatch } from 'redux';
import { fetchJokeAPI } from '../api/jokeApi';
import IStoreState from '../store/IStoreState';
import { ActionTypeKeys as keys, ActionTypes, IClearJokeAction, IFetchJokeFailAction, IFetchJokeInProgressAction, IFetchJokeSuccessAction} from './ActionTypes';

export function fetchJoke(): (dispatch: Dispatch<ActionTypes>, getState: () => IStoreState) => Promise<void> {
return async (dispatch: Dispatch<IFetchJokeInProgressAction | IFetchJokeSuccessAction | IFetchJokeFailAction>, getState: () => IStoreState) => {
dispatch(fetchJokeInProgress())
try {
const jokePayload = await fetchJokeAPI();

dispatch(fetchJokeSuccess(jokePayload));
} catch (err) {
dispatch(fetchJokeFail(err));
}
}
}

export function fetchJokeSuccess(payload: string): IFetchJokeSuccessAction {
return {
payload,
type: keys.FETCH_JOKE_SUCCESS,
}
}

export function fetchJokeInProgress(): IFetchJokeInProgressAction {
return {
payload: 'Fetching a good joke.',
type: keys.FETCH_JOKE_INPROGRESS
}
}

export function fetchJokeFail(error: Error): IFetchJokeFailAction {
return {
payload: JSON.stringify(error),
type: keys.FETCH_JOKE_FAIL
}
}

最佳答案

export function fetchJoke(): (dispatch: Dispatch<ActionTypes>, getState: () => IStoreState) => Promise<void>  {
return async (dispatch: Dispatch<IFetchJokeInProgressAction | IFetchJokeSuccessAction | IFetchJokeFailAction>, getState: () => IStoreState) => {
dispatch(fetchJokeInProgress())
try {
const jokePayload = await fetchJokeAPI();

dispatch(fetchJokeSuccess(jokePayload));
} catch (err) {
dispatch(fetchJokeFail(err));
}
}
}
由于这将是一个 thunk Action 创建器,因此您必须指定什么类型的 Action 创建器 fetchJoke()是。您正在使用 thunk Action 创建者,但您的代码中没有任何 thunk 类型。
你是说你回来了 Promise<void>但你没有 return函数中使用。
import { ThunkAction } from "redux-thunk";
import { Dispatch, ActionCreator } from "redux";

export const fetchJoke():ActionCreator<
ThunkAction<
Promise<IFetchJokeSuccessAction|IFetchJokeFailAction>, //return value
StoreState, // your app's store
null, // this is extraArgument is passed to the thunk to fetch data
IFetchJokeSuccessAction|IFetchJokeFailAction // which actions you are using in this thunk func
>
> =()=> {
return async (dispatch: Dispatch<ActionTypes>, getState: () => IStoreState) => {
dispatch(fetchJokeInProgress())
try {
const jokePayload = await fetchJokeAPI();
if(jokePayload){
// jokePayload could be undefined. setting type guard
return dispatch(fetchJokeSuccess(jokePayload));
}
} catch (err) {
return dispatch(fetchJokeFail(err));
}
}
}

type ActionTypes = IFetchJokeSuccessAction | IFetchJokeInProgressAction | IFetchJokeFailAction | IClearJokeAction;

关于reactjs - mapDispatchToProps 上的 Redux Thunk Typescript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51254730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com