Skip to content

Commit 8d03029

Browse files
sungpakschosunghoonTkDodo
authoredOct 9, 2024··
fix(react-query): Allow optional initialData in infiniteQueryOptions (#8154)
* fix(react-query): Allow optional initialData in infiniteQueryoptions * fixup! fix(react-query): Allow optional initialData in infiniteQueryoptions --------- Co-authored-by: chosunghoon <shcho@team-everywhere.com> Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent 6c13d01 commit 8d03029

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed
 

‎packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { infiniteQueryOptions } from '../infiniteQueryOptions'
44
import { useInfiniteQuery } from '../useInfiniteQuery'
55
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
66
import { useQuery } from '../useQuery'
7-
import type { InfiniteData } from '@tanstack/query-core'
7+
import type { InfiniteData, InitialDataFunction } from '@tanstack/query-core'
88

99
describe('queryOptions', () => {
1010
it('should not allow excess properties', () => {
@@ -152,4 +152,23 @@ describe('queryOptions', () => {
152152
// @ts-expect-error cannot pass infinite options to non-infinite query functions
153153
queryClient.prefetchQuery(options)
154154
})
155+
156+
test('allow optional initialData', () => {
157+
const initialData: { example: boolean } | undefined = { example: true }
158+
const queryOptions = infiniteQueryOptions({
159+
queryKey: ['example'],
160+
queryFn: async () => initialData,
161+
// initialData below errors
162+
initialData: initialData
163+
? () => ({ pages: [initialData], pageParams: [] })
164+
: undefined,
165+
getNextPageParam: () => 1,
166+
initialPageParam: 1,
167+
})
168+
queryOptions.initialData
169+
expectTypeOf(queryOptions.initialData).toMatchTypeOf<
170+
| InitialDataFunction<InfiniteData<{ example: boolean }, number>>
171+
| undefined
172+
>()
173+
})
155174
})

‎packages/react-query/src/infiniteQueryOptions.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
DataTag,
33
DefaultError,
44
InfiniteData,
5+
InitialDataFunction,
56
QueryKey,
67
} from '@tanstack/query-core'
78
import type { UseInfiniteQueryOptions } from './types'
@@ -20,7 +21,11 @@ export type UndefinedInitialDataInfiniteOptions<
2021
TQueryKey,
2122
TPageParam
2223
> & {
23-
initialData?: undefined
24+
initialData?:
25+
| undefined
26+
| InitialDataFunction<
27+
NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
28+
>
2429
}
2530

2631
type NonUndefinedGuard<T> = T extends undefined ? never : T

0 commit comments

Comments
 (0)
Please sign in to comment.