从redux-toolkit中使用createAsyncThunk创建的另一个thunk调度thunk

时间:2020-10-22 12:25:45

标签: typescript redux redux-toolkit

这是更多打字稿问题,但我的问题是@redux/toolkit的{​​{3}}方法内部的特定问题。调度重击动作的返回类型是什么?

const myService = (num: number) => Promise.resolve(num.toString());

const myAction = createAsyncThunk(
  'FOO', myService
)

const myFirstAction = createAsyncThunk<
  string, // return type
  number // param type
>('First', (num) => {
  return myService(num) // calling service directly so OK
})

const mySecondAction = createAsyncThunk<
  string, // WORKS because i'm calling unwrapResult on resolved value
  number, // param type
  {
    dispatch: AppDispatch,
  }
>('Second', (num, { dispatch }) => {
  return dispatch(myFirstAction(num)).then(unwrapResult)
})

const myThirdAction = createAsyncThunk<
  string, // FAILS ?? <- What type to use here
  number, // param type
  {
    dispatch: AppDispatch,
  }
>('Third', (num, { dispatch }) => {
  return dispatch(myFirstAction(num))
})

1 个答案:

答案 0 :(得分:0)

严格来说,是

ReturnType< AsyncThunkActionCreator<Returned, ThunkArg, ThunkApiConfig> >

但是,因为不会导出该类型,

ReturnType< AsyncThunk<
  Returned,
  ThunkArg,
  ThunkApiConfig
> >

应该可以解决问题。

您可以看到此类型here的来源。