Type error when use bindActionCreators with async thunk action
What version of React, ReactDOM/React Native, Redux, and React Redux are you using?
- React: 18.2.0
- ReactDOM/React Native: 18.2.0
- @reduxjs/toolkit: 1.8.4
- React Redux: 8.0.2
What is the current behavior?
call binded async thunk action will return AsyncThunkAction, not the return of AsyncThunkAction. Please check this code:

What is the expected behavior?
call binded async thunk action should return the result of AsyncThunkAction
Which browser and OS are affected by this issue?
No response
Did this work in previous versions of React Redux?
- [ ] Yes
BindActionCreators does not know of any Middleware you are using, so it acts as if you are not using any. It can also not take those from dispatch, as those are overloads that even include generics - and typescript has no tools for us to map over these.
Bottom line: please just don't use bindActionCreators if avoidable in any way. There are multiple reasons we are not showing that anywhere in the docs, this being one of them.
If you are dead-set on it, you will have to cast those types manually.
Thanks for the explanation. I'll try to cast it or wrap the bind function myself.
Is there any specific reason why you want to go against all recommended best practices and use this in the first place? This is a very old api and afaik we explicitly do not document it for hook usage, as we want to encourage people to actually actively use dispatch.
I want to extract a function to include the logic of useDispatch, useCallback, unwrap. They are common in our project. There is an example. The logic in useAsyncAction function are duplicated many times in our project. When I tried to extract it, I want to refer to the implementation of bindActionCreators and make the type as strong as possible.
Yeah, that's a bit too generic that TS could ever infer that - you'll have to write quite a bit of types by hand - like this:
import {
createAsyncThunk,
createReducer,
configureStore,
AsyncThunk,
Dispatch,
} from '@reduxjs/toolkit'
import {
useDispatch,
} from 'react-redux';
import React, {
useCallback,
} from 'react';
const asyncThunk = createAsyncThunk('asyncThunk', (args: { foo: string}) => args)
const reducer = createReducer({}, (builder) => {
builder.addCase(asyncThunk.fulfilled, (draft) => {
draft = {}
})
})
const store = configureStore({ reducer });
type AppDispatch = typeof store.dispatch;
const useAppDispatch: () => AppDispatch = useDispatch;
type AsyncThunkConfig = {
state?: unknown
dispatch?: Dispatch
extra?: unknown
rejectValue?: unknown
serializedErrorType?: unknown
pendingMeta?: unknown
fulfilledMeta?: unknown
rejectedMeta?: unknown
};
export const useAsyncAction = <T extends AsyncThunk<any, any, any>>(actionCreator: T)=> {
const dispatch = useAppDispatch();
return useCallback(
(args: Parameters<T>[0]): Promise<T extends AsyncThunk<infer R, any, any> ? R : never> => {
return dispatch(actionCreator(args)).unwrap()
},
[dispatch, actionCreator],
);
};
function Home() {
const onClick = useAsyncAction(asyncThunk);
return <button onClick={onClick}>Click</button>
}
Playground Link: Provided
Usually I'd probably avoid it though - writing
const x = () => dispatch(myThunk()).unwrap() is pretty straightforward and a lot more telling what is actually happening.
Thanks for your great suggestion and improvement! Could you please have a look the one last question when I want to implement the useAsyncActions just like what redux does?
This is my current implementation:
import {
createAsyncThunk,
createReducer,
configureStore,
AsyncThunk,
} from '@reduxjs/toolkit'
import {
useDispatch,
} from 'react-redux';
import React, {
useCallback,
useMemo,
} from 'react';
const asyncThunk = createAsyncThunk('asyncThunk', (args: any) => args)
const reducer = createReducer({}, (builder) => {
builder.addCase(asyncThunk.fulfilled, (draft) => {
draft = {}
})
})
const store = configureStore({ reducer });
type AppDispatch = typeof store.dispatch;
const useAppDispatch: () => AppDispatch = useDispatch;
type GetReturned<T> = T extends AsyncThunk<infer Returned, any, any> ? Returned : never;
type GetThunkArg<T> = T extends AsyncThunk<any, infer ThunkArg, any> ? ThunkArg : never;
const bindAsyncAction = <T extends AsyncThunk<any, any, any>>(actionCreator: T, dispatch: AppDispatch) =>
(arg: GetThunkArg<T>): Promise<GetReturned<T>> => dispatch(actionCreator(arg)).unwrap();
const useAsyncAction = <T extends AsyncThunk<any, any, any>>(actionCreator: T) => {
const dispatch = useAppDispatch();
return useCallback(
bindAsyncAction(actionCreator, dispatch),
[dispatch, actionCreator],
);
};
type Params = {
[key: string]: AsyncThunk<any, any, any>
};
type Result = {
[key: string]: (args: any) => Promise<any>
};
export const useAsyncActions = <P extends Params, R extends Result>(actionCreators: P): R => {
const dispatch = useAppDispatch();
return useMemo(
() => {
const result: Result = {};
for (const key in actionCreators) {
const actionCreator = actionCreators[key];
result[key] = bindAsyncAction(actionCreator, dispatch);
}
return result;
},
[dispatch],
);
};
function Home() {
const onClick = useAsyncAction(asyncThunk);
return <button onClick={onClick}>Click</button>
}
But I got type error:

I wandering how redux can pass the type check but I got the error? Is there a way to make the similar implementation?
Thanks again!
You are working with yet-to-be-resolved generics in TS at that point - everything there are placeholders to be filled in on usage. At some point you will have to cast a few things.
That said, your code is not possibly inferrable - how would TS ever find out what R is based on P? Those are completely decoupled. See my example where I derived the output type from the input type on the type level. TS can't analyze that for you.
I tried Object.entities and upgrade the ts version, target to resolve the problem. Here is the code if anyone want to refer:
import {
createAsyncThunk,
createReducer,
configureStore,
AsyncThunk,
} from '@reduxjs/toolkit'
import {
useDispatch,
} from 'react-redux';
import React, {
useCallback,
useMemo,
} from 'react';
const asyncThunk = createAsyncThunk('asyncThunk', (args: any) => args)
const asyncThunkTwo = createAsyncThunk('asyncThunkTwo', (args: number) => args)
const reducer = createReducer({}, (builder) => {
builder.addCase(asyncThunk.fulfilled, (draft) => {
draft = {}
})
})
const store = configureStore({ reducer });
type AppDispatch = typeof store.dispatch;
const useAppDispatch: () => AppDispatch = useDispatch;
type GetReturned<T> = T extends AsyncThunk<infer Returned, any, any> ? Returned : never;
type GetThunkArg<T> = T extends AsyncThunk<any, infer ThunkArg, any> ? ThunkArg : never;
const bindAsyncAction = <T extends AsyncThunk<any, any, any>>(actionCreator: T, dispatch: AppDispatch) => (
arg: GetThunkArg<T>,
): Promise<GetReturned<T>> => dispatch(actionCreator(arg)).unwrap();
export const useAsyncAction = <T extends AsyncThunk<any, any, any>>(actionCreator: T) => {
const dispatch = useAppDispatch();
return useCallback(
bindAsyncAction(actionCreator, dispatch),
[dispatch],
);
};
type Params = {
[key: string]: AsyncThunk<any, any, any>
};
type Result<P extends Params> = {
[K in keyof P]: (args: GetThunkArg<P[K]>) => Promise<GetReturned<P[K]>>
};
export const useAsyncActions = <P extends Params, R extends Result<P>>(actionCreators: P): R => {
const dispatch = useAppDispatch();
return useMemo(
() => {
const array = Object.entries(actionCreators).map(
([key, asyncThunk]) => [key, bindAsyncAction(asyncThunk, dispatch)],
);
return Object.fromEntries(array);
},
[dispatch],
);
};
function Home() {
const onClick = useAsyncAction(asyncThunk);
const {
one,
two,
} = useAsyncActions({
one: asyncThunk,
two: asyncThunkTwo,
});
return <button onClick={onClick}>Click</button>
}
The function one and two can infer it's original args.
You don't need that R generic.
export const useAsyncActions = <P extends Params>(actionCreators: P): Result<P> => {