Typed options parameter for useQuery composables #258
-
I'm moving my queries into composables but one thing I can't figure out is how to type options without specifying the return type. // composables/useLocationQuery.ts
export const useLocationQuery = (
latitude: number,
longitude: number,
options?: Omit<
UseQueryOptions<ReverseGeolocationResponse, unknown, ReverseGeolocationResponse>, // this here
"queryKey" | "queryFn"
>
) => {
const fetcher = async () =>
await ky
.get(`https://dev.virtualearth.net/REST/v1/Locations/${latitude},${longitude}`)
.json<ReverseGeolocationResponse>();
return useQuery(["geolocation", latitude, longitude], fetcher, options);
}; If I set How can I do options cleaner/without specifying response types? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Well, in this particular example you know the return type anyway, so maybe something like this, using generic will work for you: // composables/useLocationQuery.ts
export const useLocationQuery = <TData = ReverseGeolocationResponse>(
latitude: number,
longitude: number,
options?: Omit<
UseQueryOptions<TData>, // this here
"queryKey" | "queryFn"
>
) => {
const fetcher = async () =>
await ky
.get(`https://dev.virtualearth.net/REST/v1/Locations/${latitude},${longitude}`)
.json<TData>();
return useQuery(["geolocation", latitude, longitude], fetcher, options);
}; The other thing is that ideally you should not allow to pass all options to your custom composable. But that of course depends on the use-case. |
Beta Was this translation helpful? Give feedback.
Well, in this particular example you know the return type anyway, so maybe something like this, using generic will work for you:
The other thing is that ideally you should not allow to pass all options to your custom composa…