首页 > 解决方案 > 为带有返回值的 Redux Thunk 操作添加流类型

问题描述

我有一个 thunk 动作,在调度其他动作后,返回一个boolean. 我不确定如何用 Flow 输入这个。这是动作:

export const checkAuthStatus = (): ThunkAction => async (dispatch) => {
  const token = await getFromStorage('authToken');
  const user = await getFromStorage('authUser', true);
  const promptSkipped = await getFromStorage('promptSkipped', true);

  const loggedIn = token && user;

  if (loggedIn) {
    dispatch(setLoggedIn(token, user));
    dispatch(getUserInBackground());
  }

  return promptSkipped;
};

ThunkAction返回类型来自Flow 文档中的这个片段,其中Action当前包含所有非 Thunk 操作的类型:

type GetState = () => State;
type PromiseAction = Promise<Action>;
type ThunkAction = (dispatch: Dispatch, getState: GetState) => any;
type Dispatch = (action: Action | ThunkAction | PromiseAction | Array<Action>) => any;

如果我只是调用await checkAuthStatus(),这个片段就可以工作,但是如果我尝试分配返回值,例如const skipped = await checkAuthStatus()我得到:

Cannot call await with `checkAuthStatus()` bound to `p` because  `ThunkAction` [1] is incompatible with  `Promise` [2].Flow(InferError)

任何帮助表示赞赏。


编辑这是一个可重现的例子

30当我试图将 a 添加string到 a时,我需要 line to error boolean,但正如您所看到的,它抛出了上述错误。

标签: javascriptreact-nativereduxflowtyperedux-thunk

解决方案


推荐阅读