infiniteQueryOptions
TheinfiniteQueryOptions helper provides type-safe infinite query options with proper type inference. It’s a lightweight identity function that helps TypeScript infer types correctly and provides better autocomplete for infinite queries.
Import
Signature
Type Parameters
type
The type of data returned by the query function for each page
type
default:"DefaultError"
The type of error that can be thrown by the query function
type
default:"InfiniteData<TQueryFnData>"
The type of data after transformation by the select function
type
default:"QueryKey"
The type of the query key
type
default:"unknown"
The type of the page parameter
Parameters
UseInfiniteQueryOptions
required
Infinite query configuration options. Accepts all options that
useInfiniteQuery accepts.TQueryKey
required
A unique key for the query
QueryFunction<TQueryFnData, TQueryKey>
required
The function that fetches a page of data
TPageParam
required
The initial page parameter
(lastPage: TQueryFnData, allPages: TQueryFnData[], lastPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
required
Function to get the next page parameter
(firstPage: TQueryFnData, allPages: TQueryFnData[], firstPageParam: TPageParam, allPageParams: TPageParam[]) => TPageParam | undefined | null
Function to get the previous page parameter
Returns
Returns the same options object that was passed in, with enhanced type information for better TypeScript inference.Examples
Basic Usage
Reusable Infinite Query Definitions
With Type Inference
With Select Function
With QueryClient Methods
Bidirectional Pagination
Notes
infiniteQueryOptionsis an identity function that returns the exact object passed to it.- Its primary purpose is to improve TypeScript type inference and provide better autocomplete.
- It’s particularly useful when you want to share infinite query configurations across multiple components or files.
- The function adds type annotations to the
queryKeythat can be used for type-safe query invalidation and prefetching. - Always provide
initialPageParamandgetNextPageParamfor infinite queries. - Use
getPreviousPageParamif you need bidirectional pagination.