Skip to content

Commit eba777f

Browse files
Fix for react adapter
1 parent 97f4a62 commit eba777f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/react-query/src/__tests__/queryOptions.test-d.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,23 @@ describe('queryOptions', () => {
169169
const data = queryClient.getQueryData(options.queryKey)
170170
expectTypeOf(data).toEqualTypeOf<unknown>()
171171
})
172+
173+
it('should allow undefined response in initialData', () => {
174+
return (id: string | null) =>
175+
queryOptions({
176+
queryKey: ['todo', id],
177+
queryFn: () =>
178+
Promise.resolve({
179+
id: '1',
180+
title: 'Do Laundry',
181+
}),
182+
initialData: () =>
183+
!id
184+
? undefined
185+
: {
186+
id,
187+
title: 'Initial Data',
188+
},
189+
})
190+
})
172191
})

packages/react-query/src/queryOptions.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
1+
import type {
2+
DataTag,
3+
DefaultError,
4+
InitialDataFunction,
5+
QueryKey,
6+
} from '@tanstack/query-core'
27
import type { UseQueryOptions } from './types'
38

49
export type UndefinedInitialDataOptions<
@@ -7,7 +12,7 @@ export type UndefinedInitialDataOptions<
712
TData = TQueryFnData,
813
TQueryKey extends QueryKey = QueryKey,
914
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
10-
initialData?: undefined
15+
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
1116
}
1217

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

0 commit comments

Comments
 (0)