Skip to content

Commit 4360ff4

Browse files
committed
Add JSDocs for useStore
1 parent 8f84eb9 commit 4360ff4

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/exports.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export type {
77

88
import shallowEqual from './utils/shallowEqual'
99

10+
import Provider from './components/Provider'
1011
import { defaultNoopBatch } from './utils/batch'
1112

1213
export { ReactReduxContext } from './components/Context'
1314
export type { ReactReduxContextValue } from './components/Context'
1415

1516
export type { ProviderProps } from './components/Provider'
16-
import Provider from './components/Provider'
1717

1818
export type {
1919
MapDispatchToProps,
@@ -36,6 +36,7 @@ export { createSelectorHook, useSelector } from './hooks/useSelector'
3636
export type { UseSelector } from './hooks/useSelector'
3737

3838
export { createStoreHook, useStore } from './hooks/useStore'
39+
export type { UseStore } from './hooks/useStore'
3940

4041
export type { Subscription } from './utils/Subscription'
4142

src/hooks/useDispatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface UseDispatch<
3737
*
3838
* @example
3939
* ```ts
40-
* const useAppDispatch = useDispatch.withTypes<AppDispatch>()
40+
* export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
4141
* ```
4242
*
4343
* @template OverrideDispatchType - The specific type of the dispatch function.

src/hooks/useSelector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export interface UseSelector<StateType = unknown> {
106106
*
107107
* @example
108108
* ```ts
109-
* const useAppSelector = useSelector.withTypes<RootState>()
109+
* export const useAppSelector = useSelector.withTypes<RootState>()
110110
* ```
111111
*
112112
* @template OverrideStateType - The specific type of state this hook operates on.

src/hooks/useStore.ts

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,66 @@ import {
77
useReduxContext as useDefaultReduxContext,
88
} from './useReduxContext'
99

10-
export type StoreAction<StoreType extends Store> = StoreType extends Store<
11-
any,
12-
infer ActionType
13-
>
14-
? ActionType
15-
: never
10+
/**
11+
* Represents a type that extracts the action type from a given Redux store.
12+
*
13+
* @template StoreType - The specific type of the Redux store.
14+
*
15+
* @since 9.1.0
16+
* @internal
17+
*/
18+
export type ExtractStoreActionType<StoreType extends Store> =
19+
StoreType extends Store<any, infer ActionType> ? ActionType : never
1620

21+
/**
22+
* Represents a custom hook that provides access to the Redux store.
23+
*
24+
* @template StoreType - The specific type of the Redux store that gets returned.
25+
*
26+
* @since 9.1.0
27+
* @public
28+
*/
1729
export interface UseStore<StoreType extends Store> {
30+
/**
31+
* Returns the Redux store instance.
32+
*
33+
* @returns The Redux store instance.
34+
*/
1835
(): StoreType
1936

37+
/**
38+
* Returns the Redux store instance with specific state and action types.
39+
*
40+
* @returns The Redux store with the specified state and action types.
41+
*
42+
* @template StateType - The specific type of the state used in the store.
43+
* @template ActionType - The specific type of the actions used in the store.
44+
*/
2045
<
2146
StateType extends ReturnType<StoreType['getState']> = ReturnType<
2247
StoreType['getState']
2348
>,
24-
ActionType extends Action = StoreAction<Store>
49+
ActionType extends Action = ExtractStoreActionType<Store>
2550
>(): Store<StateType, ActionType>
2651

52+
/**
53+
* Creates a "pre-typed" version of {@linkcode useStore useStore}
54+
* where the type of the Redux `store` is predefined.
55+
*
56+
* This allows you to set the `store` type once, eliminating the need to
57+
* specify it with every {@linkcode useStore useStore} call.
58+
*
59+
* @returns A pre-typed `useStore` with the store type already defined.
60+
*
61+
* @example
62+
* ```ts
63+
* export const useAppStore = useStore.withTypes<AppStore>()
64+
* ```
65+
*
66+
* @template OverrideStoreType - The specific type of the Redux store that gets returned.
67+
*
68+
* @since 9.1.0
69+
*/
2770
withTypes: <
2871
OverrideStoreType extends StoreType
2972
>() => UseStore<OverrideStoreType>

0 commit comments

Comments
 (0)