Skip to content

Commit

Permalink
Add JSDocs for useStore
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaemami59 committed Jan 5, 2024
1 parent 8f84eb9 commit 4360ff4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export type {

import shallowEqual from './utils/shallowEqual'

import Provider from './components/Provider'
import { defaultNoopBatch } from './utils/batch'

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

export type { ProviderProps } from './components/Provider'
import Provider from './components/Provider'

export type {
MapDispatchToProps,
Expand All @@ -36,6 +36,7 @@ export { createSelectorHook, useSelector } from './hooks/useSelector'
export type { UseSelector } from './hooks/useSelector'

export { createStoreHook, useStore } from './hooks/useStore'
export type { UseStore } from './hooks/useStore'

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

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface UseDispatch<
*
* @example
* ```ts
* const useAppDispatch = useDispatch.withTypes<AppDispatch>()
* export const useAppDispatch = useDispatch.withTypes<AppDispatch>()
* ```
*
* @template OverrideDispatchType - The specific type of the dispatch function.
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export interface UseSelector<StateType = unknown> {
*
* @example
* ```ts
* const useAppSelector = useSelector.withTypes<RootState>()
* export const useAppSelector = useSelector.withTypes<RootState>()
* ```
*
* @template OverrideStateType - The specific type of state this hook operates on.
Expand Down
57 changes: 50 additions & 7 deletions src/hooks/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,66 @@ import {
useReduxContext as useDefaultReduxContext,
} from './useReduxContext'

export type StoreAction<StoreType extends Store> = StoreType extends Store<
any,
infer ActionType
>
? ActionType
: never
/**
* Represents a type that extracts the action type from a given Redux store.
*
* @template StoreType - The specific type of the Redux store.
*
* @since 9.1.0
* @internal
*/
export type ExtractStoreActionType<StoreType extends Store> =
StoreType extends Store<any, infer ActionType> ? ActionType : never

/**
* Represents a custom hook that provides access to the Redux store.
*
* @template StoreType - The specific type of the Redux store that gets returned.
*
* @since 9.1.0
* @public
*/
export interface UseStore<StoreType extends Store> {
/**
* Returns the Redux store instance.
*
* @returns The Redux store instance.
*/
(): StoreType

/**
* Returns the Redux store instance with specific state and action types.
*
* @returns The Redux store with the specified state and action types.
*
* @template StateType - The specific type of the state used in the store.
* @template ActionType - The specific type of the actions used in the store.
*/
<
StateType extends ReturnType<StoreType['getState']> = ReturnType<
StoreType['getState']
>,
ActionType extends Action = StoreAction<Store>
ActionType extends Action = ExtractStoreActionType<Store>
>(): Store<StateType, ActionType>

/**
* Creates a "pre-typed" version of {@linkcode useStore useStore}
* where the type of the Redux `store` is predefined.
*
* This allows you to set the `store` type once, eliminating the need to
* specify it with every {@linkcode useStore useStore} call.
*
* @returns A pre-typed `useStore` with the store type already defined.
*
* @example
* ```ts
* export const useAppStore = useStore.withTypes<AppStore>()
* ```
*
* @template OverrideStoreType - The specific type of the Redux store that gets returned.
*
* @since 9.1.0
*/
withTypes: <
OverrideStoreType extends StoreType
>() => UseStore<OverrideStoreType>
Expand Down

0 comments on commit 4360ff4

Please sign in to comment.