Skip to content

Commit

Permalink
Fix for react adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
ShacharHarshuv authored and sharshuv-quotient committed May 8, 2024
1 parent 4f20910 commit 4cfa6ff
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/react-query/src/__tests__/queryOptions.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,23 @@ describe('queryOptions', () => {
const data = queryClient.getQueryData(options.queryKey)
expectTypeOf(data).toEqualTypeOf<unknown>()
})

it('should allow undefined response in initialData', () => {
return (id: string | null) =>
queryOptions({
queryKey: ['todo', id],
queryFn: () =>
Promise.resolve({
id: '1',
title: 'Do Laundry',
}),
initialData: () =>
!id
? undefined
: {
id,
title: 'Initial Data',
},
})
})
})
9 changes: 7 additions & 2 deletions packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core'
import type {
DataTag,
DefaultError,
InitialDataFunction,
QueryKey,
} from '@tanstack/query-core'
import type { UseQueryOptions } from './types'

export type UndefinedInitialDataOptions<
Expand All @@ -7,7 +12,7 @@ export type UndefinedInitialDataOptions<
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
> = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
initialData?: undefined
initialData?: undefined | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
}

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

0 comments on commit 4cfa6ff

Please sign in to comment.