gpt4 book ai didi

javascript - Typescript:如何在 Redux 中输入 Dispatch

转载 作者:搜寻专家 更新时间:2023-10-30 20:56:58 24 4
gpt4 key购买 nike

例如,我想在此处删除 dispatch: any:

export const fetchAllAssets = () => (dispatch: any) => {
dispatch(actionGetAllAssets);
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}

我在上面分派(dispatch)了 2 个 Action ,actionsGetAllAssetsactionsSetAllassets

以下是两者的接口(interface)和 actionCreators:

// Interfaces
interface IActions {
GET_ALL_ASSETS: string;
SET_ALL_ASSETS: string;
GET_MARKET_PRICES: string;
SET_MARKET_PRICES: string;
ADD_COIN_PORTFOLIO: string;
ADD_COINS_PORTFOLIO: string;
UPDATE_COIN_PORTFOLIO: string;
REMOVE_COIN_PORTFOLIO: string;
}

interface IGetAllAssets {
type: IActions['GET_ALL_ASSETS'];
loading: boolean;
}

interface ISetAllAssets {
type: IActions['SET_ALL_ASSETS'];
assets: IAsset[];
loading: boolean;
}

// ACTION CREATORS
const actionGetAllAssets = () => ({
type: Actions.GET_ALL_ASSETS,
loading: true
});

const actionSetAllAssets = (data: any) => ({
type: Actions.SET_ALL_ASSETS,
assets: data,
loading: false
});

然后我尝试了以下方法:

export const fetchAllAssets = () => (dispatch: IGetAllAssets | ISetAllAssets) => {
console.log('fetchAllAssets', dispatch);
dispatch(actionGetAllAssets);
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}

但是它会产生这个 Typescript 错误:

Cannot invoke an expression whose type lacks a call signature. Type 'IGetAllAssets | ISetAllAssets' has no compatible call signatures.ts(2349)

想法?或者是否有不同的方式来输入调度事件?

最佳答案

Redux 类型有一个通用的 Dispatch<A>您可以使用的类型,其中 A是 Action 的类型。

export const fetchAllAssets = () => (dispatch: Dispatch<IGetAllAssets | ISetAllAssets>) => {
// ...
}

关于javascript - Typescript:如何在 Redux 中输入 Dispatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54844839/

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