Replies: 3 comments 21 replies
-
the idea that I've had for v5 is the following: we have a new function
this one is fully typed just like
so, its an abstraction to share the options needed to run a query. At runtime, it's just an identity function: it returns the options you pass in: query/packages/react-query/src/queryOptions.ts Lines 56 to 58 in 3f057a2 50 lines of types, 1 line of runtime code 😅 So with that, I'd expect most users who want the best type safety to define their query options with that function. Now what if we'd take that a step further and we would allow passing
This would basically be the upfront definition that we'd need to make Thoughts? |
Beta Was this translation helpful? Give feedback.
-
here's a first poc in that direction, thanks to @Andarist 🙏 |
Beta Was this translation helpful? Give feedback.
-
I made a PR (#6227) to add this new functionality to vue-query as it's currently only available on react-query. I also investigated a bit more about the issue of the approach described here it seems related a typescript inference limitations when working with nested extends. There seems to be no solutions at least in current state of TS, maybe we would be able it work with future releases of TS. Also my PR on Vue is implemented based on current type for QueryKey based on the first poc from @Andarist so if we want to migrate to my suggestion from PR #6201 it will need an update to make the Vue code working in the new approach. So keep me touch if you want to switch to my type suggestion and I'll make the update. |
Beta Was this translation helpful? Give feedback.
-
Hey, I would like to share here an Idea of a way to implement type inferrence of data from the query key or mutation key in the QueryClient functions getQueryData, setQueryData...
This Idea comes from the implementation of types inference for the functionality provide/inject in Vue.
The Idea is to define the key as an exported const with a typing hosting the type information of the query and then inferring the getQueryData type from the type of the key if possible:
Usage would look something like that:
Of course with this solution using a key directly as ['todos'] without using the shared TodosKey will not infer typings as currently but it would offer a way for users to type there queries as much as possible.
Maybe other currently untyped methods could also benefits from this patern.
My example is written for Vue as I'm currently working with the Vue package of TanstackQuery but this pattern could be applied to all implementations.
If this solution is considered a good solution I would be able to take a bit of time to make a PR implementing this.
Waiting for your feedbacks :)
Ps: see https://vuejs.org/guide/typescript/composition-api.html#typing-provide-inject for my inspiration source in Vue provide/inject mechanism
Beta Was this translation helpful? Give feedback.
All reactions