-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
base: v4
Are you sure you want to change the base?
Conversation
View your CI Pipeline Execution ↗ for commit 1f290f2.
☁️ Nx Cloud last updated this comment at |
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:
|
75dc26f
to
bbe7f73
Compare
…d options from useSuspenseQuery
@@ -15,7 +15,7 @@ env: | |||
|
|||
jobs: | |||
main: | |||
name: Nx Cloud | |||
name: Nx Cloud - Main Job |
There was a problem hiding this comment.
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
export function useQuery< | ||
TQueryFnData = unknown, | ||
TError = unknown, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
>( | ||
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
): DefinedUseQueryResult<TData, TError> |
There was a problem hiding this comment.
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, | ||
TData = TQueryFnData, | ||
TQueryKey extends QueryKey = QueryKey, | ||
>( | ||
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, | ||
): UseQueryResult<TData, TError> |
There was a problem hiding this comment.
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
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>>() | ||
}) | ||
}) | ||
}) |
There was a problem hiding this comment.
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
fix #9240