Type error when using `keepPreviousData()` with `useInfiniteQuery`
When setting up an infinite query that looks something like:
there's a type error with
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,
});
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-roseOP•8mo ago
(due to post character limit, here are the other two overload errors)
How to fix this error?
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.
generous-apricot•8mo ago
initialData: keepPreviousData,that doesn't exist you want
placeholderData: keepPreviousDatafair-roseOP•8mo ago
My refactoring brain was short circuited -- i fixed all the others and stumbled with this one, silly mistake on my end, thanks!