Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow optional initialData object in queryOptions #8162

Merged
merged 4 commits into from
Oct 11, 2024
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 @@ -158,14 +158,12 @@ describe('infiniteQueryOptions', () => {
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>
Expand All @@ -178,14 +176,12 @@ describe('infiniteQueryOptions', () => {
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>
Expand Down
20 changes: 19 additions & 1 deletion packages/react-query/src/__tests__/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { queryOptions } from '../queryOptions'
import { useQuery } from '../useQuery'
import { useQueries } from '../useQueries'
import { useSuspenseQuery } from '../useSuspenseQuery'
import type { QueryObserverResult } from '@tanstack/query-core'
import type {
InitialDataFunction,
QueryObserverResult,
} from '@tanstack/query-core'

describe('queryOptions', () => {
it('should not allow excess properties', () => {
Expand Down Expand Up @@ -205,4 +208,19 @@ describe('queryOptions', () => {
},
})
})

it('should allow optional initialData object', () => {
const testFn = (id?: string) => {
const options = queryOptions({
queryKey: ['test'],
queryFn: async () => 'something string',
initialData: id ? 'initial string' : undefined,
})
expectTypeOf(options.initialData).toMatchTypeOf<
InitialDataFunction<string> | string | undefined
>()
}
testFn('id')
testFn()
})
})
1 change: 1 addition & 0 deletions packages/react-query/src/infiniteQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export type DefinedInitialDataInfiniteOptions<
initialData:
| NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
| (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
| undefined
}

export function infiniteQueryOptions<
Expand Down
5 changes: 4 additions & 1 deletion packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export type UndefinedInitialDataOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
initialData?:
| undefined
| InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
| NonUndefinedGuard<TQueryFnData>
}

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