需要打字稿和 createAsyncThunk 的建议

时间:2021-01-26 19:06:58

标签: typescript redux redux-toolkit

我正在学习创建切片来处理我的所有状态和异步操作。我正在复制 the docs 以尝试执行此操作,但打字稿出现错误。

我的代码:

export const fetchTrackData = createAsyncThunk(
  "posts/fetchAllTracksData",
  async (user: any, data: any, spotifyToken: any, thunkApi: any) => {
    try {
      if (!spotifyToken)
        return thunkApi.rejectWithValue({
          type: CustomError.NO_SPOTIFY_TOKEN,
        });
      const response = await fetchSpotifyData(user, data, spotifyToken);
      return response;
    } catch (err) {
      thunkApi.rejectWithValue({
        type: CustomError.ERROR_FETCHING_SPOTIFY_DATA,
        reason: err.response,
      });
    }
  }
);

错误:

Argument of type '(user: any, data: any, spotifyToken: any, thunkApi: any) => Promise<any>' is not assignable to parameter of type 'AsyncThunkPayloadCreator<any, void, {}>'.ts(2345)

1 个答案:

答案 0 :(得分:2)

我认为您的回调 (payloadCreator) 的签名不正确, 在文档中,它声明两个值被传递给函数。

'payloadCreator 函数将使用两个参数调用:'

https://redux-toolkit.js.org/api/createAsyncThunk