Create a typed dispatch hook
React component dispatching actions and thunks
Redux Toolkit · Type Alias
AppDispatch is the store-specific TypeScript type for Redux dispatch. Derive it from typeof store.dispatch so middleware-provided capabilities, including thunk dispatching, are represented.
AppDispatch is a TypeScript alias derived directly from the configured Redux store's dispatch function. Because configureStore incorporates the store's middleware into that function type, the alias can correctly type dispatching thunks and other middleware-supported values when supplied to a pre-typed React-Redux hook.
export type AppDispatch = typeof store.dispatchThe dispatch function type inferred from the configured Redux store, including correctly typed middleware additions.
import { configureStore } from '@reduxjs/toolkit'
import { useDispatch } from 'react-redux'
const store = configureStore({ reducer: {} })
export type AppDispatch = typeof store.dispatch
export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
store.dispatch({ type: 'app/started' })Create the alias from the configured store, then use it to define a pre-typed dispatch hook.
React component dispatching actions and thunks
Stores with thunk or other middleware
Shared TypeScript application setup
Define it as typeof store.dispatch after creating the store. This lets TypeScript infer the dispatch type, including changes introduced by configured middleware such as thunk.
Follow-up: What if middleware is customized? The inferred type reflects correctly typed middleware supplied through the store configuration.
Use AppDispatch with a pre-typed dispatch hook, such as useDispatch.withTypes<AppDispatch>(). This gives components the store-specific dispatch behavior rather than the base hook type.
Follow-up: Why not annotate every component separately? A shared pre-typed hook centralizes the correct type and avoids repeatedly supplying it.
The alias is derived from the configured store instead of manually listing action types. Consequently, dispatch supports the middleware-handled values that the store's dispatch function accepts.
Follow-up: What enables thunk dispatching? The thunk middleware contributes its dispatch overloads to the inferred store dispatch type.
RootStateuseAppDispatchuseDispatchconfigureStoremiddlewarethunk middleware