Skip to content

Commit 3ffa3a6

Browse files
committed
Update docs to avoid circular type
1 parent cf29e62 commit 3ffa3a6

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

docs/rtk-query/usage/persistence-and-rehydration.mdx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,27 @@ import { REHYDRATE } from 'redux-persist'
4343

4444
type RootState = any // normally inferred from state
4545

46-
function isHydrateAction(action: Action): action is PayloadAction<RootState> {
46+
function isHydrateAction(action: Action): action is Action<typeof REHYDRATE> & {
47+
key: string
48+
payload: RootState
49+
err: unknown
50+
} {
4751
return action.type === REHYDRATE
4852
}
4953

5054
export const api = createApi({
5155
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
5256
// highlight-start
53-
extractRehydrationInfo(action, { reducerPath }) {
57+
// to prevent circular type issues, the return type needs to be annotated as any
58+
extractRehydrationInfo(action, { reducerPath }): any {
5459
if (isHydrateAction(action)) {
55-
if ((action as any).key === 'key used with redux-persist') {
56-
// when persisting the api reducer
60+
// when persisting the api reducer
61+
if (action.key === 'key used with redux-persist') {
5762
return action.payload
5863
}
5964

6065
// When persisting the root reducer
61-
return action.payload[reducerPath]
66+
return action.payload[api.reducerPath]
6267
}
6368
},
6469
// highlight-end

packages/toolkit/src/query/createApi.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,18 @@ export interface CreateApiOptions<
175175
*
176176
* type RootState = any; // normally inferred from state
177177
*
178-
* function isHydrateAction(action: Action): action is PayloadAction<RootState> {
179-
* return action.type === HYDRATE
178+
* function isHydrateAction(action: Action): action is Action<typeof REHYDRATE> & {
179+
* key: string
180+
* payload: RootState
181+
* err: unknown
182+
* } {
183+
* return action.type === REHYDRATE
180184
* }
181185
*
182186
* export const api = createApi({
183187
* baseQuery: fetchBaseQuery({ baseUrl: '/' }),
184188
* // highlight-start
185-
* extractRehydrationInfo(action, { reducerPath }) {
189+
* extractRehydrationInfo(action, { reducerPath }): any {
186190
* if (isHydrateAction(action)) {
187191
* return action.payload[reducerPath]
188192
* }

0 commit comments

Comments
 (0)