Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vue ref is unwrapped in queryKey - Vue: No overload matches this call. #96

Open
egonm12 opened this issue Aug 7, 2024 · 1 comment
Open

Comments

@egonm12
Copy link

egonm12 commented Aug 7, 2024

When a ref is passed as a query key in combination with this package it throws a typescript error on useQuery:

Vue: No overload matches this call.
Overload 1 of 3,
(options: UndefinedInitialQueryOptions<string, Error, string, readonly ["value", "someQuery", string, Ref<string, string>]>, queryClient?: QueryClient | undefined): UseQueryReturnType<...>
, gave the following error.
Overload 2 of 3,
(options: DefinedInitialQueryOptions<string, Error, string, readonly ["value", "someQuery", string, Ref<string, string>]>, queryClient?: QueryClient | undefined): UseQueryDefinedReturnType<...>
, gave the following error.
Overload 3 of 3, '(options: UseQueryOptions<string, Error, string, string, readonly ["value", "someQuery", string, Ref<string, string>]>, queryClient?: QueryClient | undefined): UseQueryReturnType<...>', gave the following error.

This happens because the type of Ref is Ref<string> but it gets transformed to string under the hood causing the type mismatch. When I directly pass the args to useQuery it doesn't cause any problems. Same for when the ref is passed to queryKeys as ref.value it works fine.

import {
    createQueryKeys,
    mergeQueryKeys,
} from "@lukemorales/query-key-factory";
import {unref, type Ref, toValue,} from "vue";

export const valueQueries = createQueryKeys("value", {
    someQuery: (val: Ref<string>) => {
        return {
            queryKey: ["byTool", val],
            queryFn: async () => `My value is ${unref(val)}`,
        };
    },
    someOtherQuery: (val: Ref<string>) => {
        return {
            queryKey: ["byTool", toValue(val)] as const,
            queryFn: async () => `My value is ${unref(val)}`,
        };
    },
});

export const queries = mergeQueryKeys(valueQueries);
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import TheWelcome from './components/TheWelcome.vue'
import {useQuery} from "@tanstack/vue-query";
import {queries} from "@/queries";
import {ref, unref} from "vue";

const val = ref('')

// works fine
const appQuery = useQuery({
  queryKey: ["byTool", val],
  queryFn: async () => `My value is ${unref(val)}`,
});

// error!
const someQuery = useQuery(queries.value.someQuery(val))

// works fine
const someOtherQuery = useQuery(queries.value.someOtherQuery(val))
</script>

Example repository: https://github.com/egonm12/vue-query-key-factory

Copy link

linear bot commented Aug 7, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant