打字稿:如何在Redux中键入Dispatch

问题描述 投票:2回答:2

例如,我想在这里删除dispatch: any

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

我上面发出了两个动作,actionsGetAllAssetsactionsSetAllassets

以下是两者的接口和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错误:

无法调用类型缺少调用签名的表达式。输入'IGetAllAssets | ISetAllAssets'没有兼容的呼号signatures.ts(2349)

思考?或者是否有不同的方式来键入调度事件?

javascript typescript redux react-redux dispatch
2个回答
3
投票

Redux打字机有一个你可以使用的通用Dispatch<A>类型,其中A是动作类型。

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

1
投票

我进一步了!

Dispatch是一个事件函数,所以这个工作:

interface IAllAssets {
  type: IActions['GET_ALL_ASSETS'];
  assets?: IAsset[];
  loading: boolean;
}

// ACTIONS
// Fetch assets from Nomics API V1.
export const fetchAllAssets = () => (dispatch: (arg: IAllAssets) => (IAllAssets)) =>
{
   dispatch(actionGetAllAssets());
   return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
       dispatch(actionSetAllAssets(formatAssets(responses))));
}

但是,我仍然想创建一个dispatch type,类似于:

interface IAllAssetsDispatch {
  dispatch: (arg: IAllAssets) => (IAllAssets)
}

export const fetchAllAssets = () => (dispatch: IAllAssetsDispatch) => {

但这会产生相同的缺少呼叫签名错误。

得到它了!

忘了type这是我需要使用而不是interface的功能:

type DispatchAllAssets = (arg: IAllAssets) => (IAllAssets);

type DispatchMarketPrices = (arg: ISetMarket) => (ISetMarket);

type DispatchAddCoin = (arg: ICoinPortfolio) => (ICoinPortfolio);

type DispatchAddCoins = (arg: ICoinsPortfolio) => (ICoinsPortfolio);

// ACTIONS
// Fetch assets from Nomics API V1.
export const fetchAllAssets = () => (dispatch: DispatchAllAssets) => {
  dispatch(actionGetAllAssets());
  return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
    dispatch(actionSetAllAssets(formatAssets(responses))));
}

// Fetch USD, USDC & USDT markets to filter out Exchange List.
export const fetchMarketPrices = (asset: string) => (dispatch: DispatchMarketPrices) => {
  dispatch(actionGetMarketPrices);
  return getMarkets().then((res) => {
    if (res && res.marketUSD && res.marketUSDC && res.marketUSDT) {
      const exchangesForAsset = combineExchangeData(asset, res);
      return dispatch(actionSetMarketPrices(exchangesForAsset));
    }
    else {
      return dispatch(actionSetMarketPrices([]));
    }
  });
}
© www.soinside.com 2019 - 2024. All rights reserved.