Conditional select parameter on useQuery with type inference #8355
-
I'm using v4 of TanStack Query for React on a project that integrates with a lot of different endpoints. For each endpoint, I create a file and implement a hook that uses export function useOrders<T = GetOrdersResponse>(
params: GetOrdersUrlParams,
options: {
isDisabled?: boolean;
select?: (data: GetOrdersResponse) => T;
} = {}
) {
const { isDisabled = false, select } = options;
return useQuery({
queryKey: queryKeyFactory.getOrders(params),
queryFn: () => fetchGetOrdersEndpoint(params),
enabled: !isDisabled,
select,
});
} This works great and all types are inferred properly when I pass a function defaultSelect(data: GetOrdersResponse) {
return data.rows;
} When I change the
I assume the return type of I know this is a bit of a niche use case, but any help or insight would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I would do it like this: |
Beta Was this translation helpful? Give feedback.
I was able to achieve what I wanted with function overloads like this.