Skip to content

Commit

Permalink
fix: allow optional initialData object (#8157)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominik Dorfmeister <[email protected]>
  • Loading branch information
jimmycallin and TkDodo authored Oct 9, 2024
1 parent eb341fc commit 329b5f8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('queryOptions', () => {
queryClient.prefetchQuery(options)
})

test('allow optional initialData', () => {
test('allow optional initialData function', () => {
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
Expand All @@ -168,6 +168,27 @@ describe('queryOptions', () => {
queryOptions.initialData
expectTypeOf(queryOptions.initialData).toMatchTypeOf<
| InitialDataFunction<InfiniteData<{ example: boolean }, number>>
| InfiniteData<{ example: boolean }, number>
| undefined
>()
})

test('allow optional initialData object', () => {
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
queryFn: async () => initialData,
// initialData below errors
initialData: initialData
? { pages: [initialData], pageParams: [] }
: undefined,
getNextPageParam: () => 1,
initialPageParam: 1,
})
queryOptions.initialData
expectTypeOf(queryOptions.initialData).toMatchTypeOf<
| InitialDataFunction<InfiniteData<{ example: boolean }, number>>
| InfiniteData<{ example: boolean }, number>
| undefined
>()
})
Expand Down
1 change: 1 addition & 0 deletions packages/react-query/src/infiniteQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type UndefinedInitialDataInfiniteOptions<
> & {
initialData?:
| undefined
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
| InitialDataFunction<
NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
>
Expand Down

0 comments on commit 329b5f8

Please sign in to comment.