Skip to content

Add NoInfer to useQuery return types #8654

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
55 changes: 54 additions & 1 deletion packages/react-query/src/__tests__/useQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -127,6 +127,59 @@ describe('initialData', () => {
})
})

describe('TData type inference', () => {
it('no inference of TData from return annotations', () => {
const _testFn = (): UseQueryResult<string, unknown> => {
// @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<string, unknown> = useQuery({
queryKey: [],
queryFn: () => 5,
})
})

it('correct or superset type annotations produce no type errors', () => {
const _testFn = (): UseQueryResult<number | string, unknown> => {
return useQuery({
queryKey: [],
queryFn: () => 5,
})
}

expectTypeOf(_testFn()['data']).toEqualTypeOf<string | number>()

const _val: UseQueryResult<string | number, unknown> = useQuery({
queryKey: [],
queryFn: () => 5,
})

expectTypeOf(_val['data']).toEqualTypeOf<string | number | undefined>()
})

it('usage of select function still changes generic inference', () => {
const result = useQuery({
queryKey: [],
queryFn: () => 5,
select: () => 'foo',
})

expectTypeOf(result['data']).toEqualTypeOf<string | undefined>()

const _result2 = useQuery<number, unknown, string, unknown[]>({
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({
Expand Down
13 changes: 9 additions & 4 deletions packages/react-query/src/useQuery.ts
Original file line number Diff line number Diff 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
Loading