• 1
  • 新人请关照

redux-thunk中使用ts异步action如何做类型推断

export interface DelUserInfoAction {
  type: typeof DEL_USER_INFO
}
export type UserInfoActionTypes =  DelUserInfoAction 

export function userInfoReducer(state = initUserState, action: UserInfoActionTypes): iUserInfoState {
  switch (action.type) {
    case DEL_USER_INFO:
      return { ...initUserState }
    default:
      return state
  }
}


// action
export function deleteUserInfo(): UserInfoActionTypes {
  return {
    type: DEL_USER_INFO
  }
}

export const asyncLogout = ():ThunkAction<void, RootState, null, Action<string>> => async (dispatch) => {
  await apiLogout()
  dispatch(deleteUserInfo())
}

// 组件中
const btnClick = () => {
    store.dispatch(asyncLogout)
}
// 报错
Argument of type 'ThunkAction<void, CombinedState<{ user: iUserInfoState; }>, null, Action<DelUserInfoAction>>' is not assignable to parameter of type 'AnyAction'.
阅读 67
评论
    0 个回答
    撰写回答

    登录后参与交流、获取后续更新提醒

    相似问题
    推荐文章