-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Unable to pass queryOptions to QueriesObserver (TypeScript error) #7446
Comments
Not sure if it should work or if it works coincidentally. The types for
to
works. On the other hand, I don't think it's expected that this works; if you're only depending on the core, you don't have access to |
Sorry, I did not specify it in the initial issue, but I'm using both actually In our case we can have the same requests triggered from a react component or outside of a component, and we try to share as much code as possible between the different methods. Here's an example of what I'm are trying to accomplish (where I have 3 ways to fetch the same data: with a function, with a React hook or within an observable (I'm using RxJS). import { QueryClient } from '@tanstack/query-core';
import { queryOptions, useQuery, QueryObserver } from '@tanstack/react-query';
import { Subject } from 'rxjs';
export const queryClient = new QueryClient();
// My shared query options
export const getMyDataQueryOptions = () =>
queryOptions({
queryKey: ['myData'],
queryFn: async () => {
// Fetch data
// ...
return 'myData';
},
});
// Fetch data outside of a component
export const getMyData = () => queryClient.fetchQuery(getMyDataQueryOptions());
// Fetch data inside of a component
export const useMyData = () => useQuery(getMyDataQueryOptions());
// Fetch data as an observable
let subject: Subject<string>;
export const $getMyData = () => {
if (subject) {
return subject;
}
subject = new Subject();
const observer = new QueryObserver(queryClient, getMyDataQueryOptions());
observer.subscribe(result => {
if (!result.isSuccess) {
return;
}
subject.next(result.data);
});
return subject;
}; (using As you can see being able to reuse
I didn't even notice it. Is there a reason for this? This function is also useful outside of React (see the
I can believe you on this 😅
When you say that is it because it is too complex and you do not have the time for this or because that something you do not want for some architectural reason? (just to know if you would be interested by a PR if I'm able to make this work 😇 ) |
usually not necessary because I don't know rxjs much but are you unsubscribing the observer correctly?
The type are slightly different for each adapter
A bit of both probably 😂 . If the widening to |
Noted 👍
No, but I do not want to unsubscribe (our project is a bit particular on this point, but I think this is not important for our problem here 🙂)
That's a good explanation (it raise a lot of other questions, but I'll open a discussion because that's a bit out of scope here 😅).
Haha. Ok 😄. The widening to |
ah, if we miss inference then it's no good :/ |
By missing inference I mean that in the playground above the types of (instead of |
ah okay, then let's do that to make it at least passable ... |
Widen the `QueriesObserver` queries type so we can assign `UseQueryOptions` to `QueryObserverOptions`. With this change we can now pass the result from the `queryOptions` helper to `QueriesObserver`.
Widen the `QueriesObserver` queries type so we can assign `UseQueryOptions` to `QueryObserverOptions`. The result from the `queryOptions` helper can now be passed to `QueriesObserver`.
Widen the `QueriesObserver` queries type so we can assign `UseQueryOptions` to `QueryObserverOptions`. The result from the `queryOptions` helper can now be passed to `QueriesObserver`.
* fix(query-core): widen QueriesObserver queries type (#7446) Widen the `QueriesObserver` queries type so we can assign `UseQueryOptions` to `QueryObserverOptions`. The result from the `queryOptions` helper can now be passed to `QueriesObserver`. * chore: prettier * fix(types): QueryKey needs to default to `any`, too --------- Co-authored-by: Yannick CROISSANT <[email protected]> Co-authored-by: Dominik Dorfmeister <[email protected]>
Describe the bug
When passing options created with the
queryOptions
helper toQueriesObserver
we got the following TypeScript error as it is unable to assignUseQueryOptions
toQueryObserverOptions
:Your minimal, reproducible example
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRQK4FMoE8DCAbYdAOxgBoUNsB5AIwGdMA3TctTQu2hqZqcgR0pYqYGMAhE6cAL5wAZlAgg4AcgACMAIaStAYwDWAeijpNumAFpBmLCoBQd3RLrxr2fIRJwAvHCLoAdwobD2IYAAoASgBuBycdOBh0FzZsHzg3YVFxSXCEOzgMoQBpdCwALjgAbRUQW3IVfTKVAF1SAqKbADEiSs06LCJdOCifAD5VOrhGTTwMFXbpSLjneDqu4CgXLiZMdP8g1OF6XahwzNCSciSUoRiOwsfCx1W4OoBldHiAEx2ePd8B2CUA4f145yElzI1RuMCOLXuTyRyJRqLR6IxhkMcAAenA6AALCCoPDfPwQeCYRRQFYJOoAFQJm1+J3+UH2gWBoNZ4IuBDC5Cq+UKmVKFWqtXqqiatjaHUyPT6AyGI0i40mWGms3miwR0SAA
Steps to reproduce
In the playground, see that
QueryObserver
acceptstestQuery
as options and thatmyFirstObserver
type is correctly inferred toQueryObserver<string, Error, string, string, string[]>
.However
QueriesObserver
errors when passingtestQuery
as options. If we inline the object then we have no errors.Also
QueryObserverResult
is not properly inferred (TData
is alwaysunknown
) even when passing the options as an inline object (I can open another issue for this point if needed).No issue at runtime, the code is working as expected.
Expected behavior
We should be able to pass the result of the
queryOptions
helper toQueriesObserver
and haveQueryObserverResult
properly inferred.Quoting the documentation on https://tanstack.com/query/latest/docs/reference/QueriesObserver#queriesobserver:
So, from what I understand, the
queryOptions
helper here should work too (and as you can see in the playground above, it actually works withQueryObserver
, but notQueriesObserver
).How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
react-query
TanStack Query version
v5.36.2
TypeScript version
v5.4.5
Additional context
No response
The text was updated successfully, but these errors were encountered: