question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Poor types for createAsyncThunk

See original GitHub issue

Currently attempting to type parts of createAsyncThunk to not have repeated code within the action.

Currently there are many issues open with attempting to extend the function or wrap it. I have seen some good examples but no solid way to type the thunk api that is passed into the createAsyncThunk.

My current issue is that I am unable to even type the function rejectWithValue since the type RejectWithValue type isn’t exported I am unable to type a custom function to take that type of a function. Let alone trying to type the thunkApi parameter itself which has many many unexported types that make it up.

Please export the types

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Aug 10, 2022

Sure - could you try filing a PR to update the docs with the info you’d like to see added?

1reaction
phryneascommented, Aug 10, 2022

Probably you want to write something like


function handleError<R>(thunkApi: { rejectWithValue(value: string) => R  }): R {
  return (error) => thunkApi.rejectWithValue(error.message)
}

  return axiosInstance.request(config)
    .then((res: AxiosResponse<IPokemonList>) => {
      return res.data
    })
    .catch(handleError(thunkApi))

You don’t need all the properties of thunkApi typed correctly for that. For your function, all that matters is that it receives an object with a rejectWithValue function on it that accepts a string, and that your function will return whatever that rejectWithValue returns.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are some common Redux Toolkit's CreateAsyncThunk ...
Well you can divide Async Thunk functions and Reducer functions into a separate types. They aren't exactly the same and you should notice ......
Read more >
typeActionCreator is undefined for createAsyncThunk.withTypes
withTypes<AsyncThunkConfig>() in order to have types available in my slices. Expected behaviour: I expected to be able to access the ThunkApi ...
Read more >
createAsyncThunk - Redux Toolkit
A function that accepts a Redux action type string and a callback function that should return a promise. It generates promise lifecycle action ......
Read more >
Reasoning behind using Redux Toolkit's createSlice ... - Medium
Reasoning behind using Redux Toolkit's createSlice and createAsyncThunk to handle your asynchronous state changes.
Read more >
Does it make sense to use createAsyncThunk if you aren't ...
I wrote a thunk using `createAsyncThunk` which takes a single argument, fetches some other data from my store using the `getState` param, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found