From f1f659260aed3d3be6f8d1ff65c87857302f6f0c Mon Sep 17 00:00:00 2001 From: Damian Osipiuk Date: Thu, 27 Jun 2024 00:24:31 +0200 Subject: [PATCH] fix(vue-query): types should now properly accept computed queryOptions (#7631) --- .../src/__tests__/useQuery.test-d.ts | 47 ++++++++++++++++++- packages/vue-query/src/types.ts | 4 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/vue-query/src/__tests__/useQuery.test-d.ts b/packages/vue-query/src/__tests__/useQuery.test-d.ts index 5eb555d31c..7c04dcf47d 100644 --- a/packages/vue-query/src/__tests__/useQuery.test-d.ts +++ b/packages/vue-query/src/__tests__/useQuery.test-d.ts @@ -1,12 +1,12 @@ import { describe, expectTypeOf, it } from 'vitest' -import { reactive } from 'vue-demi' +import { computed, reactive, ref } from 'vue-demi' import { useQuery } from '../useQuery' import { queryOptions } from '../queryOptions' import { simpleFetcher } from './test-utils' import type { OmitKeyof } from '..' import type { UseQueryOptions } from '../useQuery' -describe('initialData', () => { +describe('useQuery', () => { describe('Config object overload', () => { it('TData should always be defined when initialData is provided as an object', () => { const { data } = reactive( @@ -225,4 +225,47 @@ describe('initialData', () => { } }) }) + + describe('accept ref options', () => { + it('should accept ref options', () => { + const options = ref({ + queryKey: ['key'], + queryFn: simpleFetcher, + }) + + const query = reactive(useQuery(options)) + + if (query.isSuccess) { + expectTypeOf(query.data).toEqualTypeOf() + } + }) + + it('should accept computed options', () => { + const options = computed(() => ({ + queryKey: ['key'], + queryFn: simpleFetcher, + })) + + const query = reactive(useQuery(options)) + + if (query.isSuccess) { + expectTypeOf(query.data).toEqualTypeOf() + } + }) + + it('should accept computed query options', () => { + const options = computed(() => + queryOptions({ + queryKey: ['key'], + queryFn: simpleFetcher, + }), + ) + + const query = reactive(useQuery(options)) + + if (query.isSuccess) { + expectTypeOf(query.data).toEqualTypeOf() + } + }) + }) }) diff --git a/packages/vue-query/src/types.ts b/packages/vue-query/src/types.ts index b850d77a14..9c0a96adb2 100644 --- a/packages/vue-query/src/types.ts +++ b/packages/vue-query/src/types.ts @@ -1,4 +1,4 @@ -import type { Ref, UnwrapRef } from 'vue-demi' +import type { ComputedRef, Ref, UnwrapRef } from 'vue-demi' type Primitive = string | number | boolean | bigint | symbol | undefined | null type UnwrapLeaf = @@ -12,7 +12,7 @@ type UnwrapLeaf = | Set | WeakSet -export type MaybeRef = Ref | T +export type MaybeRef = Ref | ComputedRef | T export type MaybeRefOrGetter = MaybeRef | (() => T)