Skip to content
Source
Sign inSign up
TanStack/query

[vue-query] `placeholderData` has TypeScript error for generic data since v5 #6381

Send to my agent
Closedvisualjerk(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe 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

  1. hmm seems to be because of the NonFunctionGuard added 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 to vue-query, my fix doesn't work because I get an additional Ref error that I don't know much about 😬

  2. TkDodo added labelsbug

  3. TkDodo added labelstypes

  4. TkDodo added labelsv5

  5. TkDodo added labelspackage: query-core

  6. Thanks for looking into this! I was able to recreate the correct type for placeholderData with vue-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 UseQueryOptions and 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-query it 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-query itself.

  7. Yeah NonFunctionGuard was added to fix some type inference issues. I might revisit it to figure out a better way, but for now using UseQueryOptions to pick the proper type seems like the best option.

Sign in to join this conversation.