Leo*_*ban 16 javascript dispatch typescript redux react-redux
例如我想删除dispatch: any这里:
export const fetchAllAssets = () => (dispatch: any) => {
dispatch(actionGetAllAssets);
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}
Run Code Online (Sandbox Code Playgroud)
我在上面发送了 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
});
Run Code Online (Sandbox Code Playgroud)
然后我尝试了以下方法:
export const fetchAllAssets = () => (dispatch: IGetAllAssets | ISetAllAssets) => {
console.log('fetchAllAssets', dispatch);
dispatch(actionGetAllAssets);
return fetchAll([getPrices(), getAvailableSupply()]).then((responses) =>
dispatch(actionSetAllAssets(formatAssets(responses))));
}
Run Code Online (Sandbox Code Playgroud)
但是它会产生这个 Typescript 错误:
无法调用类型缺少调用签名的表达式。输入'IGetAllAssets | ISetAllAssets' 没有兼容的调用 signatures.ts(2349)
想法?或者是否有不同的方式来输入调度事件?
Den*_*ton 11
ReduxDispatch<A>类型具有您可以使用的通用类型,其中A是操作类型。
export const fetchAllAssets = () => (dispatch: Dispatch<IGetAllAssets | ISetAllAssets>) => {
// ...
}
Run Code Online (Sandbox Code Playgroud)
我又有点远了!
调度是一个事件函数,所以让它工作:
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))));
}
Run Code Online (Sandbox Code Playgroud)
不过我仍然想创建一个dispatch type类似的东西:
interface IAllAssetsDispatch {
dispatch: (arg: IAllAssets) => (IAllAssets)
}
export const fetchAllAssets = () => (dispatch: IAllAssetsDispatch) => {
Run Code Online (Sandbox Code Playgroud)
但这同样会产生缺少调用签名的错误。
忘记了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([]));
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22835 次 |
| 最近记录: |