Options inference broken when using Omit<UseQueryOptions<IUser, Error, IUser>, 'queryKey'> #8401
Send to my agentDescribe 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#
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
TkDodo added labelstypes
TkDodo added labelspackage: vue-query
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 {}Running into this problem as well - omitting keys from
UseQueryOptionsconverts 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!
This is not a bug in the types itself, but the way you try to omit the options from interface that is wrapped in
MaybeReftype MaybeRef<T> = Ref<T, T> | T | ComputedRef<T>When creating the type you should unwrap ref before
Omithappens withUnwrapRefinterface UseRequstQuery { url: Ref<string>; options: Omit<UnwrapRef<UseQueryOptions<IUser, Error, IUser>>, 'queryKey'>; }
Sign in to join this conversation.
Also occurs if you omit a key that doesn't exist
That's very weird. The issue does not affect the equivalent UseQueryOptions type in React Query, but does affect Vue Query, so seems to be something to do with the Vue types
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbwFBzgVwM4FMCKatQCeANCnAKrZ4GEDyYMwEAdhqQL5wBmUEIcAcgACMAIasxAYwDWAegBu+ALQBHfEQEBuJKEixEcAEpYuxOMAzHTcKCbicefQYqwCkO5jAJdRkrHABJSgJEMmZRECwALjgMGChgZgBzUlRRJOi4ZjQQACMCVLhRABNi2wwMGLiE5KR2d0SvKB8-CmxjNTjqIlDUNCgAGxirAB5qxKSAPm1UWVk4CAYmVhjg7rollgwRoOwoMwBRKF59wOCoabI5hc2VuFoQYBgRtfUNxi2d88Pj6DNdgiTMwCNQ0ADSWEIAkuqEWHzuDyeLyob3o8O2ANORxO-3OQMEzAgMAAFhNodp6kgsAAPPTwSRbeCYLAdTAwc5wAC8cAAFMg+oNCnDlhg6qt2lhOjB1gBKLmTXo2LAwfrMdAomhfPY-E6TPlkVCgogQwgxADaAlsoLi5wEZn6AwAuoVDW8AGLMGKiDCEZiSXlyzkK-moUO2FVQNWiADuoiecAACrxHtgAHTlCADeRYfWhvNZCKZATFcTMKEu-PpTIAZgADBW8yUylgKjEAAZN8qVOAAEgQDvYbYNofYMuH7Arqanwq2ZFHdXcDIkRVKXa5Sq4PIExJgMDAlTm1KP1NTDJAAjHS7i6pC3OZrJte1z6EGXtXLbYZBndxDqDndRlIA