T
TanStack8mo ago
fair-rose

Type error when using `keepPreviousData()` with `useInfiniteQuery`

When setting up an infinite query that looks something like:
useInfiniteQuery({
queryKey: ['...'],
queryFn,
enabled,
retry,
initialPageParam: 0,
getNextPageParam,
staleTime: 60_000, // 1 min
initialData: keepPreviousData,
});
useInfiniteQuery({
queryKey: ['...'],
queryFn,
enabled,
retry,
initialPageParam: 0,
getNextPageParam,
staleTime: 60_000, // 1 min
initialData: keepPreviousData,
});
there's a type error with initialData. The error looks like:
No overload matches this call.
Overload 1 of 3, '(options: DefinedInitialDataInfiniteOptions<MyType, unknown, InfiniteData<MyType, unknown>, (string | string[] | undefined)[], number>, queryClient?: QueryClient | undefined): DefinedUseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type '((InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>>) & (InfiniteData<...> | (() => InfiniteData<...>))) | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> & (() => InfiniteData<MyType, number>)'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number>'.
No overload matches this call.
Overload 1 of 3, '(options: DefinedInitialDataInfiniteOptions<MyType, unknown, InfiniteData<MyType, unknown>, (string | string[] | undefined)[], number>, queryClient?: QueryClient | undefined): DefinedUseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type '((InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>>) & (InfiniteData<...> | (() => InfiniteData<...>))) | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> & (() => InfiniteData<MyType, number>)'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number>'.
3 Replies
fair-rose
fair-roseOP8mo ago
(due to post character limit, here are the other two overload errors)
Overload 2 of 3, '(options: UndefinedInitialDataInfiniteOptions<MyType, unknown, InfiniteData<MyType, unknown>, (string | string[] | undefined)[], number>, queryClient?: QueryClient | undefined): UseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>> | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InitialDataFunction<InfiniteData<MyType, number>>'.
Target signature provides too few arguments. Expected 1 or more, but got 0.
Overload 3 of 3, '(options: UseInfiniteQueryOptions<MyType, unknown, InfiniteData<MyType, unknown>, MyType, (string | ... 1 more ... | undefined)[], number>, queryClient?: QueryClient | undefined): UseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>> | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InitialDataFunction<InfiniteData<MyType, number>>'.
Target signature provides too few arguments. Expected 1 or more, but got 0.
Overload 2 of 3, '(options: UndefinedInitialDataInfiniteOptions<MyType, unknown, InfiniteData<MyType, unknown>, (string | string[] | undefined)[], number>, queryClient?: QueryClient | undefined): UseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>> | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InitialDataFunction<InfiniteData<MyType, number>>'.
Target signature provides too few arguments. Expected 1 or more, but got 0.
Overload 3 of 3, '(options: UseInfiniteQueryOptions<MyType, unknown, InfiniteData<MyType, unknown>, MyType, (string | ... 1 more ... | undefined)[], number>, queryClient?: QueryClient | undefined): UseInfiniteQueryResult<...>', gave the following error.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InfiniteData<MyType, number> | InitialDataFunction<InfiniteData<MyType, number>> | undefined'.
Type '<T>(previousData: T | undefined) => T | undefined' is not assignable to type 'InitialDataFunction<InfiniteData<MyType, number>>'.
Target signature provides too few arguments. Expected 1 or more, but got 0.
How to fix this error?
generous-apricot
generous-apricot8mo ago
initialData: keepPreviousData,
that doesn't exist you want placeholderData: keepPreviousData
fair-rose
fair-roseOP8mo ago
My refactoring brain was short circuited -- i fixed all the others and stumbled with this one, silly mistake on my end, thanks!

Did you find this page helpful?