Skip to content
Source
Sign inSign up
TanStack/query

Options inference broken when using Omit<UseQueryOptions<IUser, Error, IUser>, 'queryKey'> #8401

Send to my agent
Closedhu-ke(GitHub profile, opens in a new tab) opened this issue 4 comments

Describe the bug

Description:

When using Omit<UseQueryOptions<IUser, Error, IUser>, 'queryKey'> for defining options in a wrapper function, TypeScript fails to infer the correct types for properties inside options. For example, the select option does not show any type inference.

Code Example

import {
  useQuery,
  UseQueryOptions,
} from '@tanstack/vue-query';
import { Ref, ref } from 'vue';

interface IUser {
  name: string;
  age: number;
  address: string;
}

interface UseRequstQuery {
  url: Ref<string>;
  options: Omit<UseQueryOptions<IUser, Error, IUser>, 'queryKey'>;
}

export const useRequstUser = ({
  url,
  options
}: UseRequstQuery) => {
  return useQuery<IUser, Error>({
    queryKey: ['requestUser', url],
    queryFn: async () => {
      return await Promise.resolve({
        name: 'danny',
        age: 30,
        address: `address: ${url.value}`
      });
    },
    ...options
  });
};

const address = ref('https://xxxx.com');
const user = useRequstUser({
  url: address,
  options: {
    sele // No type inference here
  }
});

Your minimal, reproducible example

https://www.typescriptlang.org/play/?ssl=18&ssc=14&pln=18&pc=27#code/JYWwDg9gTgLgBAbwFBzgVwM4FMCKatQCeANCnAKrZ4GEDyYMwEAdhqQL5wBmUEIcAcgACMAIasxAYwDWAegBu+ALQBHfEQEBuJKEixEcAEpYuxOMAzHTcKCbicefQYqwCkO5jAJdRkrHABJSgJEMmZRECwALjgMGChgZgBzUlRRJOi4ZjQQACMCVLhRABNi2wwMGLiE5KR2d0SvKB8-CmxjNTjqIlDUNCgAGxirAB5qxKSAPm1UCAYmVhjaEGAYEeDuunmWDBGg7CgzAFEoXkPA4KhJswE1GgBpLEIBabr3LAAPPXhJHfhMLAdTAwS5wAC8cAAFMg+oNCnNGDs6jFgkCuupCABKcGTXo2LAwfrMdBUDF7S7HU7QSbQsioO5ER6EGIAbQEtjucUuAjM-QGAF1CvSMQAxZgxUQYQjMSRQ7Fg3Ew1DK2yEqDE0QAd1EqzgAAVeCtsAA6coQAbyLC05U2rIRTICYriZjPIW29KZADMAAY3TaSmUsBUYgADAPlSpwAAkCD57BDdOV7ExifYbuNGYRCwwZGTbyQvwkRVKEfB+K4kIEAAsYDAwJVZLIPs2PsbfiABCnC3ESSEIQC0SCDtb0IMJSWg2wyFmdjElahsAMsLm6pigA

Steps to reproduce

/

Expected behavior

The options parameter in useRequstUser should provide type inference for fields like select

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

macOS Chrome

Tanstack Query adapter

None

TanStack Query version

5.62.2

TypeScript version

No response

Additional context

No response

  1. TkDodo added labelstypes

  2. TkDodo added labelspackage: vue-query

  3. I'm also encountering this issue, hope it will be fixed soon

    type QueryConfig<TData, TParams extends MyParams> = Omit<UseQueryOptions<TData>, "queryKey" | "queryFn"> & Partial<{ params: TParams }>;
    
    function useAssets({ params, ...queryConfig }: QueryConfig<Assets, GetAssetsParams> = {}) {
      return useQuery({
        queryKey: ["assets", params],
        queryFn: () => api.get<Response<Assets>>("/assets", { params }).then(({ data }) => data.data),
        ...queryConfig,
      });
    }
    
    // ...queryConfig type is {}
  4. Running into this problem as well - omitting keys from UseQueryOptions converts the type into {}.

    This issue makes it difficult to create type-safe custom composables with vue-query in the same way I can create type-safe custom hooks with react-query.

    Any ideas as to why this is occurring and/or workarounds? Thanks!

Sign in to join this conversation.