@@ -5,11 +5,45 @@ import type { ReactReduxContextValue } from '../components/Context'
55import { ReactReduxContext } from '../components/Context'
66import { createStoreHook , useStore as useDefaultStore } from './useStore'
77
8+ /**
9+ * Represents a custom hook that provides a dispatch function
10+ * from the Redux store.
11+ *
12+ * @template DispatchType - The specific type of the dispatch function.
13+ *
14+ * @since 9.1.0
15+ * @public
16+ */
817export interface UseDispatch <
918 DispatchType extends Dispatch < UnknownAction > = Dispatch < UnknownAction >
1019> {
20+ /**
21+ * Returns the dispatch function from the Redux store.
22+ *
23+ * @returns The dispatch function from the Redux store.
24+ *
25+ * @template AppDispatch - The specific type of the dispatch function.
26+ */
1127 < AppDispatch extends DispatchType = DispatchType > ( ) : AppDispatch
1228
29+ /**
30+ * Creates a "pre-typed" version of {@linkcode useDispatch useDispatch}
31+ * where the type of the `dispatch` function is predefined.
32+ *
33+ * This allows you to set the `dispatch` type once, eliminating the need to
34+ * specify it with every {@linkcode useDispatch useDispatch} call.
35+ *
36+ * @returns A pre-typed `useDispatch` with the dispatch type already defined.
37+ *
38+ * @example
39+ * ```ts
40+ * const useAppDispatch = useDispatch.withTypes<AppDispatch>()
41+ * ```
42+ *
43+ * @template OverrideDispatchType - The specific type of the dispatch function.
44+ *
45+ * @since 9.1.0
46+ */
1347 withTypes : <
1448 OverrideDispatchType extends DispatchType
1549 > ( ) => UseDispatch < OverrideDispatchType >
@@ -22,10 +56,15 @@ export interface UseDispatch<
2256 * @returns {Function } A `useDispatch` hook bound to the specified context.
2357 */
2458export function createDispatchHook <
25- S = unknown ,
26- A extends Action = UnknownAction
59+ StateType = unknown ,
60+ ActionType extends Action = UnknownAction
61+ > (
2762 // @ts -ignore
28- > ( context ?: Context < ReactReduxContextValue < S , A > | null > = ReactReduxContext ) {
63+ context ?: Context < ReactReduxContextValue <
64+ StateType ,
65+ ActionType
66+ > | null > = ReactReduxContext
67+ ) {
2968 const useStore =
3069 context === ReactReduxContext ? useDefaultStore : createStoreHook ( context )
3170
@@ -38,7 +77,7 @@ export function createDispatchHook<
3877 withTypes : ( ) => useDispatch ,
3978 } )
4079
41- return useDispatch as UseDispatch < Dispatch < A > >
80+ return useDispatch as UseDispatch < Dispatch < ActionType > >
4281}
4382
4483/**
0 commit comments