diff --git a/.changeset/fix-query-options-types-fix.md b/.changeset/fix-query-options-types-fix.md new file mode 100644 index 0000000000..8a8b494d96 --- /dev/null +++ b/.changeset/fix-query-options-types-fix.md @@ -0,0 +1,5 @@ +--- +'@tanstack/vue-query': patch +--- + +fix(vue-query): expose queryFn and other properties on queryOptions return type diff --git a/packages/vue-query/src/__tests__/queryOptions.test-d.ts b/packages/vue-query/src/__tests__/queryOptions.test-d.ts index 65d49d945f..ce63a08a28 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).toMatchTypeOf< + (() => Promise) | undefined + >() + expectTypeOf(options.staleTime).toMatchTypeOf() + 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..343bdfbcc0 100644 --- a/packages/vue-query/src/queryOptions.ts +++ b/packages/vue-query/src/queryOptions.ts @@ -1,8 +1,42 @@ -import type { DataTag, DefaultError, QueryKey } from '@tanstack/query-core' +import type { + DataTag, + DefaultError, + QueryFunction, + QueryKey, +} from '@tanstack/query-core' import type { DefinedInitialQueryOptions, UndefinedInitialQueryOptions, } from './useQuery' +import type { DeepUnwrapRef } from './types' + +/** + * Augmented version of UndefinedInitialQueryOptions that explicitly exposes + * queryFn and other properties for direct TypeScript access. + */ +export type UndefinedInitialDataOptions< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> = UndefinedInitialQueryOptions & { + queryKey: TQueryKey + queryFn?: QueryFunction> +} + +/** + * Augmented version of DefinedInitialQueryOptions that explicitly exposes + * queryFn and other properties for direct TypeScript access. + */ +export type DefinedInitialDataOptions< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> = DefinedInitialQueryOptions & { + queryKey: TQueryKey + queryFn?: QueryFunction> +} export function queryOptions< TQueryFnData = unknown, @@ -10,8 +44,8 @@ export function queryOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( - options: DefinedInitialQueryOptions, -): DefinedInitialQueryOptions & { + options: DefinedInitialDataOptions, +): DefinedInitialDataOptions & { queryKey: DataTag } @@ -21,8 +55,8 @@ export function queryOptions< TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( - options: UndefinedInitialQueryOptions, -): UndefinedInitialQueryOptions & { + options: UndefinedInitialDataOptions, +): UndefinedInitialDataOptions & { queryKey: DataTag }