T
TanStack•3y ago
skilled-lime

useInfiniteQuery not inferring types

on typescript 5.3.2 on @tanstack/react-query@5.8.4 the following is not inferring the correct types for useInfiniteQuery result
import { useInfiniteQuery } from '@tanstack/react-query';
function Example () {
const result = useInfiniteQuery({
getNextPageParam: (lastPage, allPages, lastPageParam) => {
return lastPageParam + 1;
},
initialPageParam: 0,
queryFn: async ({ pageParam = 0 }): Promise<PersonApiResponse> => {
return { data: [], page: pageParam, total: mockData.length } as PersonApiResponse;
},
queryKey: ['some-data'],
});
}
import { useInfiniteQuery } from '@tanstack/react-query';
function Example () {
const result = useInfiniteQuery({
getNextPageParam: (lastPage, allPages, lastPageParam) => {
return lastPageParam + 1;
},
initialPageParam: 0,
queryFn: async ({ pageParam = 0 }): Promise<PersonApiResponse> => {
return { data: [], page: pageParam, total: mockData.length } as PersonApiResponse;
},
queryKey: ['some-data'],
});
}
the type i get is
const result: UseInfiniteQueryResult<InfiniteData<unknown, unknown>, Error>
const result: UseInfiniteQueryResult<InfiniteData<unknown, unknown>, Error>
does anyone know what's up? - useQuery infers type correctly (e.g. if i only use queryFn/queryKey as above) - @tanstack/react-query@4.x infers type correctly - lowering versions @5.0 and @5.4 does not resolve my issue - lowering typescript version to 4.9 does not resolve my issue - made sure vscode is using same ts version as i have installed (rather than vscode version)
8 Replies
skilled-lime
skilled-limeOP•3y ago
if i refactor queryFn out it works
adverse-sapphire
adverse-sapphire•3y ago
Show a typescript playground pls
adverse-sapphire
adverse-sapphire•3y ago
ah, I know what it is
adverse-sapphire
adverse-sapphire•3y ago
typescript inference on objects is weird sometimes. So it probably tries to infer the type TQueryFnData from getNextPageParam instead of querFn if the order is different
skilled-lime
skilled-limeOP•3y ago
damn i use a nice eslint fixable rule/plugin which orders my object attributes 😭 thanks for looking @TkDodo 🔮 i appreciate it

Did you find this page helpful?