Skip to content

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

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

Open
wants to merge 7 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
53 changes: 41 additions & 12 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ env:

jobs:
main:
name: Nx Cloud
name: Nx Cloud - Main Job
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -31,15 +31,46 @@ jobs:
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Get base and head commits for `nx affected`
uses: nrwl/nx-set-shas@v4.3.0
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
- name: Get appropriate base and head commits for `nx affected` commands
uses: nrwl/nx-set-shas@v3
with:
main-branch-name: main
main-branch-name: 'main'
- run: |
echo "BASE: ${{ env.NX_BASE }}"
echo "HEAD: ${{ env.NX_HEAD }}"
- name: Start CI Orchestrator
run: npx nx-cloud start-ci-run
- name: Run Tests
run: pnpm run test:pr
- name: Stop Agents
run: npx nx-cloud stop-all-agents
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
agents:
name: Nx Cloud - Agents
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
agent: [1, 2, 3]
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
with:
fetch-depth: 0
- name: Setup pnpm
uses: pnpm/action-setup@v4.1.0
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version-file: .nvmrc
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
- name: Install dependencies
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
- name: Start Nx Agent ${{ matrix.agent }}
run: npx nx-cloud start-agent
format:
name: Format
runs-on: ubuntu-latest
Expand All @@ -57,7 +88,7 @@ jobs:
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
- name: Install dependencies
run: pnpm install --frozen-lockfile
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
- name: Run prettier
run: pnpm run test:format
test-react-17:
Expand All @@ -78,17 +109,15 @@ jobs:
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Get base and head commits for `nx affected`
uses: nrwl/nx-set-shas@v4.3.0
with:
main-branch-name: main
run: pnpm --filter "./packages/**" --filter query --prefer-offline install
- name: Derive appropriate SHAs for base and head for `nx affected` commands
uses: nrwl/nx-set-shas@v2
- name: Run Tests
uses: nick-fields/retry@v2.8.3
with:
timeout_minutes: 5
max_attempts: 3
command: pnpm nx affected --targets=test:lib
command: npx nx affected --targets=test:lib --base=${{ github.event.pull_request.base.sha }}
env:
NX_CLOUD_DISTRIBUTED_EXECUTION: false
REACTJS_VERSION: 17
5 changes: 5 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import type { Logger } from './logger'

export type NonUndefinedGuard<T> = T extends undefined ? never : T

export type DistributiveOmit<
TObject,
TKey extends keyof TObject,
> = TObject extends any ? Omit<TObject, TKey> : never

export type OmitKeyof<
TObject,
TKey extends TStrictly extends 'safely'
Expand Down
33 changes: 33 additions & 0 deletions packages/react-query/src/__tests__/useQuery.types.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expectTypeOf } from 'expect-type'
import { useQuery } from '../useQuery'
import { doNotExecute } from './utils'
import type { DefinedUseQueryResult, UseQueryResult } from '../types'

export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
T,
Expand All @@ -11,6 +13,37 @@ export type Expect<T extends true> = T

describe('initialData', () => {
describe('Config object overload', () => {
it('onSuccess should be typed correctly', () => {
doNotExecute(() => {
expectTypeOf(
useQuery({
queryKey: ['posts'],
queryFn: async () => ({ id: 1 }),
onSuccess: (data) =>
expectTypeOf(data).toEqualTypeOf<{ id: number }>(),
}),
).toEqualTypeOf<UseQueryResult<{ id: number }, unknown>>()
expectTypeOf(
useQuery({
queryKey: ['posts'],
queryFn: async () => ({ id: 1 }),
initialData: { id: 1 },
onSuccess: (data) =>
expectTypeOf(data).toEqualTypeOf<{ id: number }>(),
}),
).toEqualTypeOf<DefinedUseQueryResult<{ id: number }, unknown>>()
expectTypeOf(
useQuery({
queryKey: ['posts'],
queryFn: async () => ({ id: 1 }),
initialData: { id: 1 },
select: (data) => data.id,
onSuccess: (data) => expectTypeOf(data).toEqualTypeOf<number>(),
}),
).toEqualTypeOf<DefinedUseQueryResult<number, unknown>>()
})
})

it('TData should always be defined when initialData is provided as an object', () => {
doNotExecute(() => {
const { data } = useQuery({
Expand Down
4 changes: 2 additions & 2 deletions packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ProhibitedQueryOptionsKeyInV5 = keyof Pick<
'useErrorBoundary' | 'suspense' | 'getNextPageParam' | 'getPreviousPageParam'
>

export type UndefinedInitialDataOptions<
type UndefinedInitialDataOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
Expand All @@ -34,7 +34,7 @@ export type UndefinedInitialDataOptions<
| NonUndefinedGuard<TQueryFnData>
}

export type DefinedInitialDataOptions<
type DefinedInitialDataOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
Expand Down
20 changes: 0 additions & 20 deletions packages/react-query/src/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
'use client'
import { QueryObserver, parseQueryArgs } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type {
DefinedInitialDataOptions,
UndefinedInitialDataOptions,
} from './queryOptions'
import type {
InitialDataFunction,
NonUndefinedGuard,
Expand All @@ -19,14 +15,6 @@ import type {
} from './types'

// HOOK
export function useQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedUseQueryResult<TData, TError>
Comment on lines -22 to -29
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

export function useQuery<
TQueryFnData = unknown,
TError = unknown,
Expand All @@ -42,14 +30,6 @@ export function useQuery<
| (() => NonUndefinedGuard<TQueryFnData>)
},
): DefinedUseQueryResult<TData, TError>
export function useQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError>
Comment on lines -45 to -52
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

export function useQuery<
TQueryFnData = unknown,
TError = unknown,
Expand Down
5 changes: 1 addition & 4 deletions packages/react-query/src/useSuspenseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { QueryObserver } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type {
DefinedQueryObserverResult,
DistributiveOmit,
OmitKeyof,
QueryKey,
} from '@tanstack/query-core'
import type { UseQueryOptions } from './types'

type DistributiveOmit<TObject, TKey extends keyof TObject> = TObject extends any
? Omit<TObject, TKey>
: never

export type UseSuspenseQueryResult<
TData = unknown,
TError = unknown,
Expand Down
4 changes: 0 additions & 4 deletions packages/vue-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,3 @@ export type VueInfiniteQueryObserverOptions<
>[Property]
>
}

export type DistributiveOmit<T, K extends keyof any> = T extends any
? Omit<T, K>
: never
8 changes: 2 additions & 6 deletions packages/vue-query/src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ import {
import { useQueryClient } from './useQueryClient'
import type { ToRefs } from 'vue-demi'
import type {
DistributiveOmit,
MutateFunction,
MutateOptions,
MutationFunction,
MutationKey,
MutationObserverOptions,
MutationObserverResult,
} from '@tanstack/query-core'
import type {
DistributiveOmit,
MaybeRef,
MaybeRefDeep,
WithQueryClientKey,
} from './types'
import type { MaybeRef, MaybeRefDeep, WithQueryClientKey } from './types'

type MutationResult<TData, TError, TVariables, TContext> = DistributiveOmit<
MutationObserverResult<TData, TError, TVariables, TContext>,
Expand Down
Loading
Loading