From ba016fabf0a544632f6ce46329e666e1a11d9033 Mon Sep 17 00:00:00 2001 From: braeden Date: Sat, 15 Feb 2025 18:53:50 -0500 Subject: [PATCH 1/2] Add NoInfer to useQuery return types --- .../src/__tests__/useQuery.test-d.tsx | 59 ++++++++++++++++++- packages/react-query/src/useQuery.ts | 8 +-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/packages/react-query/src/__tests__/useQuery.test-d.tsx b/packages/react-query/src/__tests__/useQuery.test-d.tsx index 2e38415dec..c8a1b2eedf 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,63 @@ 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..82540df251 100644 --- a/packages/react-query/src/useQuery.ts +++ b/packages/react-query/src/useQuery.ts @@ -1,7 +1,7 @@ '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 +20,7 @@ export function useQuery< >( options: DefinedInitialDataOptions, queryClient?: QueryClient, -): DefinedUseQueryResult +): DefinedUseQueryResult, TError> export function useQuery< TQueryFnData = unknown, @@ -30,7 +30,7 @@ export function useQuery< >( options: UndefinedInitialDataOptions, queryClient?: QueryClient, -): UseQueryResult +): UseQueryResult, TError> export function useQuery< TQueryFnData = unknown, @@ -40,7 +40,7 @@ export function useQuery< >( options: UseQueryOptions, queryClient?: QueryClient, -): UseQueryResult +): UseQueryResult, TError> export function useQuery(options: UseQueryOptions, queryClient?: QueryClient) { return useBaseQuery(options, QueryObserver, queryClient) From 7b5bb5d7a47c79270136920280695e1e5e511ebb Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2025 09:46:05 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- .../src/__tests__/useQuery.test-d.tsx | 18 +++++++----------- packages/react-query/src/useQuery.ts | 7 ++++++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/react-query/src/__tests__/useQuery.test-d.tsx b/packages/react-query/src/__tests__/useQuery.test-d.tsx index c8a1b2eedf..dd4fae2d9d 100644 --- a/packages/react-query/src/__tests__/useQuery.test-d.tsx +++ b/packages/react-query/src/__tests__/useQuery.test-d.tsx @@ -128,10 +128,8 @@ describe('initialData', () => { }) describe('TData type inference', () => { - it('no inference of TData from return annotations', () => { - - const _testFn = (): UseQueryResult => { + const _testFn = (): UseQueryResult => { // @ts-expect-error expect number to be un-assignable to string return useQuery({ queryKey: [], @@ -147,39 +145,37 @@ describe('initialData', () => { }) it('correct or superset type annotations produce no type errors', () => { - - const _testFn = (): UseQueryResult => { + const _testFn = (): UseQueryResult => { return useQuery({ queryKey: [], queryFn: () => 5, }) } - expectTypeOf(_testFn()["data"]).toEqualTypeOf() + expectTypeOf(_testFn()['data']).toEqualTypeOf() const _val: UseQueryResult = useQuery({ queryKey: [], queryFn: () => 5, }) - expectTypeOf(_val["data"]).toEqualTypeOf() + expectTypeOf(_val['data']).toEqualTypeOf() }) it('usage of select function still changes generic inference', () => { - const result = useQuery({ queryKey: [], queryFn: () => 5, - select: () => "foo" + select: () => 'foo', }) - expectTypeOf(result["data"]).toEqualTypeOf() + 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 + select: () => 5, }) }) }) diff --git a/packages/react-query/src/useQuery.ts b/packages/react-query/src/useQuery.ts index 82540df251..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, NoInfer, QueryClient, QueryKey } from '@tanstack/query-core' +import type { + DefaultError, + NoInfer, + QueryClient, + QueryKey, +} from '@tanstack/query-core' import type { DefinedUseQueryResult, UseQueryOptions,