Skip to content
Source
Sign inSign up
TanStack/query

fix(react-query): correct type for `onSuccess` of `useQuery` #9242

Mergedmanudeli(GitHub profile, opens in a new tab)merged 7 commits intov4 fromrefs/pull/9242/headmerged by TkDodo View on GitHub

    1. View your CI Pipeline Execution ↗ for commit 1f290f29c9a325681cdf029fe33c0573f0b0f32a.

      CommandStatusDurationResult
      nx affected --targets=test:lib --base=e210ca1d2...✅ Succeeded<1sView ↗
      nx affected --targets=test:lib,test:types,test:...✅ Succeeded1m 17sView ↗

      ☁️ Nx Cloud last updated this comment at 2025-06-03 19:24:05 UTC

    2. This pull request is automatically built and testable in CodeSandbox.

      To see build info of the built libraries, click here or the icon next to each commit SHA.

      Latest deployment of this branch, based on commit 1f290f29c9a325681cdf029fe33c0573f0b0f32a:

      SandboxSource
      @tanstack/query-example-react-basic-typescriptConfiguration
      @tanstack/query-example-solid-basic-typescriptConfiguration
      @tanstack/query-example-svelte-basicConfiguration
      @tanstack/query-example-vue-basicConfiguration
    3. manudeli(GitHub profile, opens in a new tab) added labelspackage: react-query

    4. .github/workflows/pr.yml line 18
      @@ -15,4 +15,4 @@
      1515
      1616 jobs:
      1717 main:
      18- name: Nx Cloud
      18+ name: Nx Cloud - Main Job

      @lachlancollins I revert this workflow to ensure it runs correctly. Without this change, I encounter this CI error. https://github.com/TanStack/query/actions/runs/15422205986/job/43400032483

      packages/react-query/src/useQuery.ts lines 22 to 29
      @@ -19,11 +15,3 @@
      1915 } from './types'
      2016
      2117 // HOOK
      22-export function useQuery<
      23- TQueryFnData = unknown,
      24- TError = unknown,
      25- TData = TQueryFnData,
      26- TQueryKey extends QueryKey = QueryKey,
      27->(
      28- options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
      29-): DefinedUseQueryResult<TData, TError>

      This is unnecessary function overload, and this removal will solve our issue

      packages/react-query/src/useQuery.ts lines 45 to 52
      @@ -42,11 +30,3 @@
      4230 | (() => NonUndefinedGuard<TQueryFnData>)
      4331 },
      4432 ): DefinedUseQueryResult<TData, TError>
      45-export function useQuery<
      46- TQueryFnData = unknown,
      47- TError = unknown,
      48- TData = TQueryFnData,
      49- TQueryKey extends QueryKey = QueryKey,
      50->(
      51- options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
      52-): UseQueryResult<TData, TError>

      This is unnecessary function overload, and this removal will solve our issue

    5. packages/react-query/src/__tests__/useQuery.types.test.tsx lines 14 to 45
      @@ -11,0 +25,21 @@
      25+ expectTypeOf(
      26+ useQuery({
      27+ queryKey: ['posts'],
      28+ queryFn: async () => ({ id: 1 }),
      29+ initialData: { id: 1 },
      30+ onSuccess: (data) =>
      31+ expectTypeOf(data).toEqualTypeOf<{ id: number }>(),
      32+ }),
      33+ ).toEqualTypeOf<DefinedUseQueryResult<{ id: number }, unknown>>()
      34+ expectTypeOf(
      35+ useQuery({
      36+ queryKey: ['posts'],
      37+ queryFn: async () => ({ id: 1 }),
      38+ initialData: { id: 1 },
      39+ select: (data) => data.id,
      40+ onSuccess: (data) => expectTypeOf(data).toEqualTypeOf<number>(),
      41+ }),
      42+ ).toEqualTypeOf<DefinedUseQueryResult<number, unknown>>()
      43+ })
      44+ })
      45+})

      I added type test code to guarantee useQuery with the onSuccess option

    6. @TkDodo, could you review this? This error could be pretty painful for the latest v4 users

    7. TkDodo merged thisb886889

    8. TkDodo closed this

Sign in to join this conversation.