Skip to content

Commit 077da03

Browse files
author
ben.durrant
committed
ensure it's only possible to pass all or none of the hooks
1 parent 805379b commit 077da03

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

packages/toolkit/src/query/createApi.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ export type CreateApi<Modules extends ModuleName> = {
219219
* const MyContext = React.createContext<ReactReduxContextValue>(null as any);
220220
* const customCreateApi = buildCreateApi(
221221
* coreModule(),
222-
* reactHooksModule({ useDispatch: createDispatchHook(MyContext) })
222+
* reactHooksModule({
223+
* useDispatch: createDispatchHook(MyContext),
224+
* useSelector: createSelectorHook(MyContext),
225+
* useStore: createStoreHook(MyContext)
226+
* })
223227
* );
224228
* ```
225229
*

packages/toolkit/src/query/react/module.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
} from '@reduxjs/toolkit/dist/query/endpointDefinitions'
1010
import type { Api, Module } from '../apiTypes'
1111
import { capitalize } from '../utils'
12+
import type { AllOrNone } from '../tsHelpers'
1213
import { safeAssign } from '../tsHelpers'
1314
import type { BaseQueryFn } from '@reduxjs/toolkit/dist/query/baseQueryTypes'
1415

@@ -69,23 +70,26 @@ declare module '@reduxjs/toolkit/dist/query/apiTypes' {
6970

7071
type RR = typeof import('react-redux')
7172

72-
export interface ReactHooksModuleOptions {
73-
/**
74-
* The version of the `batchedUpdates` function to be used
75-
*/
76-
batch?: RR['batch']
73+
type ReactHooks = {
7774
/**
7875
* The version of the `useDispatch` hook to be used
7976
*/
80-
useDispatch?: RR['useDispatch']
77+
useDispatch: RR['useDispatch']
8178
/**
8279
* The version of the `useSelector` hook to be used
8380
*/
84-
useSelector?: RR['useSelector']
81+
useSelector: RR['useSelector']
8582
/**
8683
* The version of the `useStore` hook to be used
8784
*/
88-
useStore?: RR['useStore']
85+
useStore: RR['useStore']
86+
}
87+
88+
export type ReactHooksModuleOptions = AllOrNone<ReactHooks> & {
89+
/**
90+
* The version of the `batchedUpdates` function to be used
91+
*/
92+
batch?: RR['batch']
8993
/**
9094
* Enables performing asynchronous tasks immediately within a render.
9195
*
@@ -115,7 +119,11 @@ export interface ReactHooksModuleOptions {
115119
* const MyContext = React.createContext<ReactReduxContextValue>(null as any);
116120
* const customCreateApi = buildCreateApi(
117121
* coreModule(),
118-
* reactHooksModule({ useDispatch: createDispatchHook(MyContext) })
122+
* reactHooksModule({
123+
* useDispatch: createDispatchHook(MyContext),
124+
* useSelector: createSelectorHook(MyContext),
125+
* useStore: createStoreHook(MyContext)
126+
* })
119127
* );
120128
* ```
121129
*

packages/toolkit/src/query/tsHelpers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ export type Id<T> = { [K in keyof T]: T[K] } & {}
22
export type WithRequiredProp<T, K extends keyof T> = Omit<T, K> &
33
Required<Pick<T, K>>
44
export type Override<T1, T2> = T2 extends any ? Omit<T1, keyof T2> & T2 : never
5+
export type AllOrNone<T> = T | { [K in keyof T]?: never }
56
export function assertCast<T>(v: any): asserts v is T {}
67

78
export function safeAssign<T extends object>(
89
target: T,
910
...args: Array<Partial<NoInfer<T>>>
10-
) {
11-
Object.assign(target, ...args)
11+
): T {
12+
return Object.assign(target, ...args)
1213
}
1314

1415
/**

0 commit comments

Comments
 (0)