javascript - 为带有返回值的 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
,但正如您所看到的,它抛出了上述错误。
解决方案
推荐阅读
- css - 在模块样式表文件中使用类名时,样式未在 React 组件中应用
- android - 我应该如何在下面的代码中使用计时器功能来淡出edittext中显示的错误?
- botframework - Microsoft Bot Framework Composer - 发送 Slack Block Kit
- python - 从 NetworkX 动态删除节点图导致错误
- java - 如果我使用字符串作为对象的键,更改 hashCode() 是否有效?
- google-sheets - 从下拉选择 Google 表格中将“表格名称”添加到公式地址
- python - 如何在 Tkinter 中手动更新 OptionMenu 的值?
- c# - 使用 C# LINQ 返回所有数据库名称
- r - 插入符号错误说:“度量 RMSE 不适用于分类模型”,但我传递的是连续数据
- html - 在 HTML 弹出式视频播放器上激活自动播放