[vue-query] `placeholderData` has TypeScript error for generic data since v5 #6381
Send to my agentDescribe the bug#
First of all, thanks for this amazing library ❤️
When defining the returned data of queryFn and placeholderData as a generic parameter, the following TypeScript error is thrown for placeholderData:
No overload matches this call.
Overload 1 of 3, '(options: UndefinedInitialQueryOptions<TData, Error, TData, string[]>, queryClient?: QueryClient | undefined): UseQueryReturnType<...>', gave the following error.
Type 'TData' is not assignable to type 'MaybeRefDeep<NonFunctionGuard<TData> | PlaceholderDataFunction<NonFunctionGuard<TData>, Error, NonFunctionGuard<TData>, string[]> | undefined>'.
Type 'TData' is not assignable to type 'Ref<PlaceholderDataFunction<NonFunctionGuard<TData>, Error, NonFunctionGuard<TData>, string[]> | (NonFunctionGuard<...> extends Function ? Function & NonFunctionGuard<...> : NonFunctionGuard<...> extends object ? { [Property in keyof (object & NonFunctionGuard<...>)]: MaybeRefDeep<...>; } : NonFunctionGuard<...>) | ...'.
Overload 2 of 3, '(options: DefinedInitialQueryOptions<TData, Error, TData, string[]>, queryClient?: QueryClient | undefined): UseQueryDefinedReturnType<...>', gave the following error.
Type 'TData' is not assignable to type 'MaybeRefDeep<NonFunctionGuard<TData> | PlaceholderDataFunction<NonFunctionGuard<TData>, Error, NonFunctionGuard<TData>, string[]> | undefined>'.
Overload 3 of 3, '(options: UseQueryOptions<TData, Error, TData, TData, string[]>, queryClient?: QueryClient | undefined): UseQueryReturnType<TData, Error>', gave the following error.
Type 'TData' is not assignable to type 'MaybeRefDeep<NonFunctionGuard<TData> | PlaceholderDataFunction<NonFunctionGuard<TData>, Error, NonFunctionGuard<TData>, string[]> | undefined>'.(2769)
data.ts(3, 25): This type parameter might need an `extends Ref<PlaceholderDataFunction<NonFunctionGuard<TData>, Error, NonFunctionGuard<TData>, string[]> | (NonFunctionGuard<...> extends Function ? Function & NonFunctionGuard<...> : NonFunctionGuard<...> extends object ? { [Property in keyof (object & NonFunctionGuard<...>)]: MaybeRefDeep<...>; } : NonFunctionGuard<...>) | ...` constraint.
This used to work with v4.
Your minimal, reproducible example#
https://stackblitz.com/edit/vitejs-vite-vtx3f8?file=src%2Fdata.ts&terminal=dev
Steps to reproduce#
Create a generic function that uses useQuery and defines returned data as a generic parameter.
Example:
import { useQuery } from '@tanstack/vue-query';
export const useData = <TData>(
queryFn: () => Promise<TData>,
placeholderData: TData
) => {
return useQuery({
queryKey: ['key'],
queryFn,
placeholderData,
});
};
Expected behavior#
No TypeScript error is thrown.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: macOS
Tanstack Query adapter#
vue-query
TanStack Query version#
5.8.4
TypeScript version#
5.2.2
Additional context#
No response
TkDodo added labelsbug
TkDodo added labelstypes
TkDodo added labelsv5
TkDodo added labelspackage: query-core
Thanks for looking into this! I was able to recreate the correct type for
placeholderDatawithvue-query, which would look somewhat like this:import { useQuery } from '@tanstack/vue-query' import { Ref } from 'vue' type MaybeRef<T> = Ref<T> | T type MaybeRefDeep<T> = MaybeRef< T extends Function ? T : T extends object ? { [Property in keyof T]: MaybeRefDeep<T[Property]> } : T > type NonFunctionGuard<T> = T extends Function ? never : T export const useData = < TData, TPlaceholderData extends MaybeRefDeep<NonFunctionGuard<TData>> >( queryFn: () => Promise<TData>, placeholderData: TPlaceholderData ) => { return useQuery({ queryKey: ['key'], queryFn, placeholderData, }) }However a cleaner approach would be to use the exported utility type
UseQueryOptionsand derive the type from that:import { useQuery, UseQueryOptions } from '@tanstack/vue-query' import { Ref } from 'vue' export const useData = <TData>( queryFn: () => Promise<TData>, placeholderData: Exclude<UseQueryOptions<TData>, Ref>['placeholderData'] ) => { return useQuery({ queryKey: ['key'], queryFn, placeholderData, }) }Just for reference, for
react-queryit is even simpler:import { useQuery, UseQueryOptions } from '@tanstack/react-query' export const useData = <TData>( queryFn: () => Promise<TData>, placeholderData: UseQueryOptions<TData>['placeholderData'] ) => { return useQuery({ queryKey: ['key'], queryFn, placeholderData, }) }This solution is good enough for our case, so feel free to close the issue if you think there is no need for changes in
vue-queryitself.Yeah
NonFunctionGuardwas added to fix some type inference issues. I might revisit it to figure out a better way, but for now usingUseQueryOptionsto pick the proper type seems like the best option.
Sign in to join this conversation.
hmm seems to be because of the
NonFunctionGuardadded in v5. It works when that is aded to placeholderData (playground)I don't know why we added that NonFunctionGuard. I guess it's because placeholderData can itself be a function so the generic can't be one? But the same would be true for initialData, and we don't have it there
@DamianOsipiuk it seems like you added that here?
Do you know what we can do about this?
also: my playground is with
react-query. If I change that tovue-query, my fix doesn't work because I get an additionalReferror that I don't know much about 😬