-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Typescript issue with queryOptions and usePrefetchQuery #7560
Comments
You should assign generics to infer If you don't assign generics, the type of UseQueryOptions<unknown, Error, unknown, QueryKey> but the type of UseQueryOptions<string, Error, string, string[]> & {
initialData?: undefined;
} & {
queryKey: DataTag<string[], string>;
} You can see the generics are completely different each other. So you can fix the problem as follows: function usePrefetchQuery<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
const queryClient = useQueryClient()
if (!queryClient.getQueryState(options.queryKey)) {
queryClient.ensureQueryData(options).catch(() => {})
}
}
// I will work!
usePrefetchQuery(factory.example()); |
@mitchell-up oh thank you so much! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
I am attempting to add a custom
usePrefetchQuery
hook to my app based on the docs here:https://tanstack.com/query/latest/docs/framework/react/guides/prefetching
I am also using the query factory pattern where I have an object containing query data wrapped using
queryOptions
. The problem is passing one of these query options objects into my hook is causing a TS issue unless I force a cast to eitherUseQueryOptions
orEnsureQueryDataOptions
. However, callingensureQueryData
and directly passing in aqueryOptions
object works OK.The error looks like this:
I guess the type I am using for the hook
options
is incorrect but I am struggling to figure out what to use instead ofUseQueryOptions
.Your minimal, reproducible example
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgUQHYGcCuUCmBFTHKATwBEBDGcgeTBmAgwBo4BVdfQk2+x9FgI5diPBsziYOBIsQDCAG2A5U8AL5wAZlAgg4AIgACVDFQDGAawD0ucqZgBaITL0BuAFBvTfeBtsxoxHAAvIhucHA4AB7k4PI4AFxwABQAlMEAfHBO3HRi6EkIYeFZwgDSOMSJANp6ABY48vIQegC6TEXh2cQAYqiJqRlwAAraIMAcAHS46BDyAG44SXUNTXop7eGqKW6qHhqYqHZiEhwjOBo4MKa10iRJELl8ieycMqJ8aYXhXiYlMgpKFTBE6vEgA5QwVLuIrADTJACEXXBKgmAHNLrdiABlKgwRYPXgYCZdcrEFKfDp-MGKCETZRYXCYihUe6PDApCamSjXJIDIKZJBbdybHYeSyWFBQbRQNySHBnC5XG7CJK+OwBOnRWKLcnQ8VwADq0HM6Flp1wiuumNVfg1URiYDiA3I6DYUmE73ZeolRqgJrN8otlytKrV-hImodTrSLpQGGwoLIlBobPQKW9huN6Hhnm8VLkNKBITlmORkO2biRhZgdPjjOEzPISS6nvyX3zpOqy0azTaRS6vX6aX5w1G4xwUxwM3mi27q3WO11biAA
Steps to reproduce
See TS playground link
Expected behavior
Passing
queryFactory
functions intoensureQueryData
should be possible without TS errors.How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
react-query
TanStack Query version
5.40.1
TypeScript version
5.3.3
Additional context
Given how use
usePrefetchQuery
is, it would be very useful to know how to make it play nicely with TS and the query factory pattern in general.The text was updated successfully, but these errors were encountered: