Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

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'.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...