Skip to content

Commit

Permalink
fix(react-query): Allow optional initialData object in queryOptions (#…
Browse files Browse the repository at this point in the history
…8162)

* fix(react-query): Allow optional initialData in infiniteQueryoptions

* fix(react-query): Allow optional initialData object in queryOptions

* refactor(react-query): removed unnecessary lines in infiniteQueryOptions test

---------

Co-authored-by: chosunghoon <[email protected]>
  • Loading branch information
sungpaks and chosunghoon authored Oct 11, 2024
1 parent 6469385 commit 931d98d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
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

0 comments on commit 931d98d

Please sign in to comment.