Машинопись: Как набрать Диспетчер в Redux
Например, я хочу удалить dispatch: any
Вот:
export const fetchAllAssets = () => (dispatch: any) => {
dispatch(actionGetAllAssets);
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}
Есть 2 действия, которые я рассылаю выше, actionsGetAllAssets
а также actionsSetAllassets
,
Вот интерфейсы и 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)
Мысли? Или есть другой способ ввода события отправки?
2 ответа
Я получил немного дальше!
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([]));
}
});
}
Типы Redux имеют общий Dispatch<A>
тип вы можете использовать, где A
это тип действия.
export const fetchAllAssets = () => (dispatch: Dispatch<IGetAllAssets | ISetAllAssets>) => {
// ...
}