From 4360ff420a1af7b2ec3f09607ba833ebea7ae29c Mon Sep 17 00:00:00 2001 From: Arya Emami Date: Fri, 5 Jan 2024 13:27:54 -0600 Subject: [PATCH] Add JSDocs for `useStore` --- src/exports.ts | 3 ++- src/hooks/useDispatch.ts | 2 +- src/hooks/useSelector.ts | 2 +- src/hooks/useStore.ts | 57 +++++++++++++++++++++++++++++++++++----- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/exports.ts b/src/exports.ts index b6f82b512..f826f2d11 100644 --- a/src/exports.ts +++ b/src/exports.ts @@ -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, @@ -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' diff --git a/src/hooks/useDispatch.ts b/src/hooks/useDispatch.ts index f8c9737e3..011c5c7e8 100644 --- a/src/hooks/useDispatch.ts +++ b/src/hooks/useDispatch.ts @@ -37,7 +37,7 @@ export interface UseDispatch< * * @example * ```ts - * const useAppDispatch = useDispatch.withTypes() + * export const useAppDispatch = useDispatch.withTypes() * ``` * * @template OverrideDispatchType - The specific type of the dispatch function. diff --git a/src/hooks/useSelector.ts b/src/hooks/useSelector.ts index bcf7f27a1..db30fdba1 100644 --- a/src/hooks/useSelector.ts +++ b/src/hooks/useSelector.ts @@ -106,7 +106,7 @@ export interface UseSelector { * * @example * ```ts - * const useAppSelector = useSelector.withTypes() + * export const useAppSelector = useSelector.withTypes() * ``` * * @template OverrideStateType - The specific type of state this hook operates on. diff --git a/src/hooks/useStore.ts b/src/hooks/useStore.ts index 61fe42480..40a0d31e7 100644 --- a/src/hooks/useStore.ts +++ b/src/hooks/useStore.ts @@ -7,23 +7,66 @@ import { useReduxContext as useDefaultReduxContext, } from './useReduxContext' -export type StoreAction = 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 ? 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 { + /** + * 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 = ReturnType< StoreType['getState'] >, - ActionType extends Action = StoreAction + ActionType extends Action = ExtractStoreActionType >(): Store + /** + * 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() + * ``` + * + * @template OverrideStoreType - The specific type of the Redux store that gets returned. + * + * @since 9.1.0 + */ withTypes: < OverrideStoreType extends StoreType >() => UseStore