diff --git a/packages/react-query/src/__tests__/useQuery.test-d.tsx b/packages/react-query/src/__tests__/useQuery.test-d.tsx index 2e38415dec..dd4fae2d9d 100644 --- a/packages/react-query/src/__tests__/useQuery.test-d.tsx +++ b/packages/react-query/src/__tests__/useQuery.test-d.tsx @@ -2,7 +2,7 @@ import { describe, expectTypeOf, it } from 'vitest' import { useQuery } from '../useQuery' import { queryOptions } from '../queryOptions' import type { OmitKeyof } from '..' -import type { UseQueryOptions } from '../types' +import type { UseQueryOptions, UseQueryResult } from '../types' describe('initialData', () => { describe('Config object overload', () => { @@ -127,6 +127,59 @@ describe('initialData', () => { }) }) + describe('TData type inference', () => { + it('no inference of TData from return annotations', () => { + const _testFn = (): UseQueryResult => { + // @ts-expect-error expect number to be un-assignable to string + return useQuery({ + queryKey: [], + queryFn: () => 5, + }) + } + + // @ts-expect-error expect number to be un-assignable to string + const _val: UseQueryResult = useQuery({ + queryKey: [], + queryFn: () => 5, + }) + }) + + it('correct or superset type annotations produce no type errors', () => { + const _testFn = (): UseQueryResult => { + return useQuery({ + queryKey: [], + queryFn: () => 5, + }) + } + + expectTypeOf(_testFn()['data']).toEqualTypeOf() + + const _val: UseQueryResult = useQuery({ + queryKey: [], + queryFn: () => 5, + }) + + expectTypeOf(_val['data']).toEqualTypeOf() + }) + + it('usage of select function still changes generic inference', () => { + const result = useQuery({ + queryKey: [], + queryFn: () => 5, + select: () => 'foo', + }) + + expectTypeOf(result['data']).toEqualTypeOf() + + const _result2 = useQuery({ + queryKey: [], + queryFn: () => 5, + // @ts-expect-error select fn differs from generic (when provided), so correctly type errors) + select: () => 5, + }) + }) + }) + describe('structuralSharing', () => { it('should restrict to same types', () => { useQuery({ diff --git a/packages/react-query/src/useQuery.ts b/packages/react-query/src/useQuery.ts index 962ef4da66..52d479551d 100644 --- a/packages/react-query/src/useQuery.ts +++ b/packages/react-query/src/useQuery.ts @@ -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, @@ -20,7 +25,7 @@ export function useQuery< >( options: DefinedInitialDataOptions, queryClient?: QueryClient, -): DefinedUseQueryResult +): DefinedUseQueryResult, TError> export function useQuery< TQueryFnData = unknown, @@ -30,7 +35,7 @@ export function useQuery< >( options: UndefinedInitialDataOptions, queryClient?: QueryClient, -): UseQueryResult +): UseQueryResult, TError> export function useQuery< TQueryFnData = unknown, @@ -40,7 +45,7 @@ export function useQuery< >( options: UseQueryOptions, queryClient?: QueryClient, -): UseQueryResult +): UseQueryResult, TError> export function useQuery(options: UseQueryOptions, queryClient?: QueryClient) { return useBaseQuery(options, QueryObserver, queryClient)