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

Categories

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

redux - Need advice with typescript and createAsyncThunk

I am learning to create slices to handle all of my state and async actions. I am copying the docs in trying to do this but I am getting an error with typescript.

My Code:

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,
      });
    }
  }
);

The Error:

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)
question from:https://stackoverflow.com/questions/65907698/need-advice-with-typescript-and-createasyncthunk

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

1 Reply

0 votes
by (71.8m points)

I think the signature of your callback (payloadCreator) is not correct, In the documentation, it states two values are passed to the function.

'The payloadCreator function will be called with two arguments:'

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...