TanStackT
TanStack8mo ago
3 replies
sacred-rose

How to type select fn

I have next code:

export function useTimezones(select?: <T>(data: Timezone[]) => T) {
  return useQuery({
    queryKey: ['timezones'],
    queryFn: () => api.get<Timezone[]>('/timezones'),
    select,
    staleTime: Infinity,
  });
}

export const useTimezoneById = (id: number) =>
  useTimezones((timezones) => timezones.find((timezone) => timezone.id === id));

I want to properly type a select function, to make select generic, so that TS properly recognized the response of useQuery with and without select function. I keep getting an error.
image.png
Was this page helpful?