[SOLVED] Typescript issues with infinitequery and initial data
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
14 Replies
frail-apricot•7mo ago
This sorta works but I'm not sure I'd advise it
Reusability can definitely be better
optimistic-goldOP•7mo ago
Thanks, the idea is to use
typeof infiniteQueryOptions instead of useInfiniteQueryOptions
if you are free please share your ideas on that !!frail-apricot•7mo ago
It sort of depends what you're trying to do, but I personally find that helpers around the
queryOptions()/infiniteQueryOptions() functions tend to be hard to get right for little benefitoptimistic-goldOP•7mo ago
I wanna mostly use it because, the underlying queryFn supports for predictable api params
or wait not that
frail-apricot•7mo ago
I mean, that's not necessarily true… I tend to use helpers too but they're tied to a specific query and basically define everything necessary for that one query, rather than have 1 super generic and reusable helper
optimistic-goldOP•7mo ago
I can do
createQpts<Data>().queryKey to prefetch the query data during the app start
also i can pass initialData to the createQptsfrail-apricot•7mo ago
for example, for a "users" query I'll do something like this
And in places where I want to fetch users I'll then do
optimistic-goldOP•7mo ago
yeah i do something like this as well
but then if i wanna set the query with initial data, the one i device comes very handy
frail-apricot•7mo ago
Yeah I guess.. but you'll have to leverage TypeScript generics to type the underlying queryoptions correctly
optimistic-goldOP•7mo ago
not necessarily, that will be abstracted to the
createQOpts function right.frail-apricot•7mo ago
And those tend to be tricky to get right... my initial code snippet above for example is technically also incorrect because
infiniteQueryOptionscan take a union of 3 different shapes (one with initialData, one without, and one other I forgot), stuff like that tends to mess with your types
Anyway, personal preference I guess, if it works well for you all the better 🙂optimistic-goldOP•7mo ago
yeah tanstack query kinda hard to use with very very proper types
this is something i already have
oh yeah now i remember the reason for abstracting it as createQOpts
I can now use
queryClient.fetchQuery as well as useQuery({})
this way i can abstract non reactive code outside my react components
and then i can use it for prefetching as well in the start of my appfrail-apricot•7mo ago
yeah definitely, also the main reason I use my helpers like
getUsersQueryOptions, especially when you combine react-query with a router where you want to preload in a loader it's super helpful to just reuse the same helper funcoptimistic-goldOP•7mo ago
yeah
thanks @pleunv !