queryOptions
ThequeryOptions helper provides type-safe query options with proper type inference. It’s a lightweight identity function that helps TypeScript infer types correctly and provides better autocomplete.
Import
Signature
Type Parameters
type
default:"unknown"
The type of data returned by the query function
type
default:"DefaultError"
The type of error that can be thrown by the query function
type
default:"TQueryFnData"
The type of data after transformation by the select function
type
default:"QueryKey"
The type of the query key
Parameters
UseQueryOptions
required
Query configuration options. Accepts all options that
useQuery accepts.Returns
Returns the same options object that was passed in, with enhanced type information for better TypeScript inference.Examples
Basic Usage
Reusable Query Definitions
With Type Inference
With Select
With QueryClient Methods
Factory Pattern
Notes
queryOptionsis 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 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. - This pattern is recommended for organizing and reusing query configurations in larger applications.