Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { infiniteQueryOptions } from '../infiniteQueryOptions'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
import { useQuery } from '../useQuery'
import type { InfiniteData } from '@tanstack/query-core'
import type { InfiniteData, InitialDataFunction } from '@tanstack/query-core'

describe('queryOptions', () => {
it('should not allow excess properties', () => {
Expand Down Expand Up @@ -152,4 +152,23 @@ describe('queryOptions', () => {
// @ts-expect-error cannot pass infinite options to non-infinite query functions
queryClient.prefetchQuery(options)
})

test('allow optional initialData', () => {
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>>
| undefined
>()
})
})
7 changes: 6 additions & 1 deletion packages/react-query/src/infiniteQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
DataTag,
DefaultError,
InfiniteData,
InitialDataFunction,
QueryKey,
} from '@tanstack/query-core'
import type { UseInfiniteQueryOptions } from './types'
Expand All @@ -20,7 +21,11 @@ export type UndefinedInitialDataInfiniteOptions<
TQueryKey,
TPageParam
> & {
initialData?: undefined
initialData?:
| undefined
| InitialDataFunction<
NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
Copy link
Contributor

@jimmycallin jimmycallin Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this have been something like

  initialData?:
    | undefined
    | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
    | InitialDataFunction<
        NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>

to allow passing initialData as object and not within a callback function?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can pass initialData as an object via DefinedInitialDataInfiniteOptions:

> & {
initialData:
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
}

Copy link
Contributor

@jimmycallin jimmycallin Oct 9, 2024

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay then what’s the fix please 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a PR here :) #8157

Hope that clarifies things, or perhaps I'm misunderstanding something!

>
}

type NonUndefinedGuard<T> = T extends undefined ? never : T
Expand Down
Loading