T
TanStack16mo ago
cloudy-cyan

TypeScript errors for createQuery(options, queryClient)

If I do the following:
const tsqSessionQueryOptions = () => {
return queryOptions({
queryKey: ['userSession'],
queryFn: async () => {
return { session: 'fake-thing' };
},
staleTime: 1000 * 60 * 10, // 10 minutes and we don't trust it anmore
refetchInterval: 1000 * 60 * 5, // 5 minutes and we recheck if we can
retry: true,
placeholderData: keepPreviousData,
deferStream: true,
});
};

const session = createQuery(tsqSessionQueryOptions);
const tsqSessionQueryOptions = () => {
return queryOptions({
queryKey: ['userSession'],
queryFn: async () => {
return { session: 'fake-thing' };
},
staleTime: 1000 * 60 * 10, // 10 minutes and we don't trust it anmore
refetchInterval: 1000 * 60 * 5, // 5 minutes and we recheck if we can
retry: true,
placeholderData: keepPreviousData,
deferStream: true,
});
};

const session = createQuery(tsqSessionQueryOptions);
I have no error in TS. But if I change the last line to:
createQuery(tsqSessionQueryOptions, myQueryClient);
createQuery(tsqSessionQueryOptions, myQueryClient);
the typescript errors out expecting initialData to be set and ignores the non intiailized version of the type. I need to pas sin the query client since this happens outside the routing tree and contexts. 5.4.5 typescript.
1 Reply
cloudy-cyan
cloudy-cyanOP16mo ago
I can just use queryClient.fetchQuery({...}) can't I. doh

Did you find this page helpful?