T
TanStack•6mo ago
adverse-sapphire

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));
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.
No description
3 Replies

Did you find this page helpful?