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
16 changes: 16 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test-d.tsx
Original file line numberDiff line numberDiff line change
Expand Up@@ -277,6 +277,22 @@ describe('useQuery', () => {
}
})

// eslint-disable-next-line vitest/expect-expect
it('TData should depend from only arguments, not the result', () => {
// @ts-expect-error
const result: UseQueryResult<{ wow: string }> = useQuery({
queryKey: ['key'],
queryFn: () => {
return {
wow: true,
}
},
initialData: () => undefined as { wow: boolean } | undefined,
})

void result
})

it('data should not have undefined when initialData is provided', () => {
const { data } = useQuery({
queryKey: ['query-key'],
Expand Down
13 changes: 9 additions & 4 deletions packages/react-query/src/useQuery.ts
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
'use client'
import { QueryObserver } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
import type {
DefaultError,
NoInfer,
QueryClient,
QueryKey,
} from '@tanstack/query-core'
import type {
DefinedUseQueryResult,
UseQueryOptions,
Expand All@@ -20,7 +25,7 @@ export function useQuery<
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): DefinedUseQueryResult<TData, TError>
): DefinedUseQueryResult<NoInfer<TData>, TError>

export function useQuery<
TQueryFnData = unknown,
Expand All@@ -30,7 +35,7 @@ export function useQuery<
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): UseQueryResult<TData, TError>
): UseQueryResult<NoInfer<TData>, TError>

export function useQuery<
TQueryFnData = unknown,
Expand All@@ -40,7 +45,7 @@ export function useQuery<
>(
options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
): UseQueryResult<TData, TError>
): UseQueryResult<NoInfer<TData>, TError>

export function useQuery(options: UseQueryOptions, queryClient?: QueryClient) {
return useBaseQuery(options, QueryObserver, queryClient)
Expand Down