TanStackT
TanStack16mo ago
3 replies
sad-indigo

Typed cache

I was wondering if we could do something like typed "cache".
Kinda like what router does with loaders.

Our app has grown quite a bit and mistakes in form of key collision happen now.
I would like to tie query keys with data type so that mishandling would error in typescript or at least being passed down to the useQuery data type with a union.

In my opinion this would dramatically improve dx.
Especially in cases of optimistic data mutation where setQueryData is used.

The way I'm thinking of implementing it is with a wrapper around useQuery & QueryClient but I don't know if this would benefit from being package level.
Do you think this should be discussed or something of the sort is already being discussed in the github ?
Some related topics :
- https://github.com/TanStack/query/discussions/3888
- https://github.com/TanStack/query/discussions/8123
- https://tkdodo.eu/blog/type-safe-react-query#what-about-getquerydata

But It doesn't go all the way to the QueryClient for validation.
Was this page helpful?