From cc06f3cc7da0e71767d6a980f7603be19617fedf Mon Sep 17 00:00:00 2001 From: Vedant Madane <6527493+VedantMadane@users.noreply.github.com> Date: Thu, 15 Jan 2026 09:54:06 +0530 Subject: [PATCH] fix(vue-query): expose queryFn and other properties on queryOptions return type The return type of queryOptions was using types wrapped in MaybeRef, which prevented TypeScript from seeing properties like queryFn directly on the returned object. This creates new unwrapped types (DefinedInitialDataOptions and UndefinedInitialDataOptions) specifically for queryOptions return types, matching the approach used in react-query. Fixes #7892 Signed-off-by: Vedant Madane <6527493+VedantMadane@users.noreply.github.com> --- .../src/__tests__/queryOptions.test-d.ts | 15 +++++ packages/vue-query/src/queryOptions.ts | 65 ++++++++++++++++--- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/packages/vue-query/src/__tests__/queryOptions.test-d.ts b/packages/vue-query/src/__tests__/queryOptions.test-d.ts index 65d49d945f..3514a97eec 100644 --- a/packages/vue-query/src/__tests__/queryOptions.test-d.ts +++ b/packages/vue-query/src/__tests__/queryOptions.test-d.ts @@ -6,6 +6,21 @@ import { queryOptions } from '../queryOptions' import { useQuery } from '../useQuery' describe('queryOptions', () => { + it('should expose queryFn and other properties on the returned options object', () => { + const options = queryOptions({ + queryKey: ['key'], + queryFn: () => Promise.resolve(5), + staleTime: 1000, + }) + + // These should be accessible without TS errors (issue #7892) + expectTypeOf(options.queryFn).toEqualTypeOf< + (() => Promise) | undefined + >() + expectTypeOf(options.staleTime).toEqualTypeOf() + expectTypeOf(options.queryKey).toMatchTypeOf() + }) + it('should not allow excess properties', () => { assertType( queryOptions({ diff --git a/packages/vue-query/src/queryOptions.ts b/packages/vue-query/src/queryOptions.ts index 4681080f8c..c5d91a0839 100644 --- a/packages/vue-query/src/queryOptions.ts +++ b/packages/vue-query/src/queryOptions.ts @@ -1,8 +1,57 @@ -import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core' import type { - DefinedInitialQueryOptions, - UndefinedInitialQueryOptions, -} from './useQuery' + DataTag, + DefaultError, + InitialDataFunction, + NonUndefinedGuard, + QueryKey, + QueryObserverOptions, +} from '@tanstack/query-core' +import type { DeepUnwrapRef, ShallowOption } from './types' + +/** + * Options for queryOptions with defined initial data. + * These are unwrapped types (not MaybeRef) so that properties like queryFn are directly accessible. + */ +export type DefinedInitialDataOptions< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> = QueryObserverOptions< + TQueryFnData, + TError, + TData, + TQueryFnData, + DeepUnwrapRef +> & + ShallowOption & { + initialData: + | NonUndefinedGuard + | (() => NonUndefinedGuard) + } + +/** + * Options for queryOptions with undefined initial data. + * These are unwrapped types (not MaybeRef) so that properties like queryFn are directly accessible. + */ +export type UndefinedInitialDataOptions< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> = QueryObserverOptions< + TQueryFnData, + TError, + TData, + TQueryFnData, + DeepUnwrapRef +> & + ShallowOption & { + initialData?: + | undefined + | InitialDataFunction> + | NonUndefinedGuard + } export function queryOptions< TQueryFnData = unknown, @@ -10,8 +59,8 @@ export function queryOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( - options: DefinedInitialQueryOptions, -): DefinedInitialQueryOptions & { + options: DefinedInitialDataOptions, +): DefinedInitialDataOptions & { queryKey: DataTag } @@ -21,8 +70,8 @@ export function queryOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( - options: UndefinedInitialQueryOptions, -): UndefinedInitialQueryOptions & { + options: UndefinedInitialDataOptions, +): UndefinedInitialDataOptions & { queryKey: DataTag }