Skip to content

Typed options parameter for useQuery composables #258

Answered by DamianOsipiuk
RihanArfan asked this question in Q&A
Discussion options

You must be logged in to vote

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 composa…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by RihanArfan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants