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

Conversation

manudeli
Copy link
Collaborator

@manudeli manudeli commented Jun 3, 2025

fix #9240

@manudeli manudeli linked an issue Jun 3, 2025 that may be closed by this pull request
Copy link

nx-cloud bot commented Jun 3, 2025

View your CI Pipeline Execution ↗ for commit 1f290f2.

Command Status Duration Result
nx affected --targets=test:lib --base=e210ca1d2... ✅ Succeeded <1s View ↗
nx affected --targets=test:lib,test:types,test:... ✅ Succeeded 1m 17s View ↗

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

Copy link

codesandbox-ci bot commented Jun 3, 2025

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 1f290f2:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@manudeli manudeli self-assigned this Jun 3, 2025
@manudeli manudeli marked this pull request as ready for review June 3, 2025 15:47
@manudeli manudeli marked this pull request as draft June 3, 2025 15:55
@manudeli manudeli force-pushed the fix/onSuccess-type branch from 75dc26f to bbe7f73 Compare June 3, 2025 16:02
@manudeli manudeli requested review from TkDodo and lachlancollins June 3, 2025 16:09
@@ -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

Comment on lines -22 to -29
export function useQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedUseQueryResult<TData, TError>
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

Comment on lines -45 to -52
export function useQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError>
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

@manudeli manudeli marked this pull request as ready for review June 3, 2025 16:28
Comment on lines +14 to +45
describe('onSuccess', () => {
it('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>>()
})
})
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query callback function typing is broken in latest v4 release
1 participant