Skip to content

Commit

Permalink
fix(vue-query): types should now properly accept computed queryOptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianOsipiuk authored Jun 26, 2024
1 parent 52cc7b2 commit f1f6592
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
47 changes: 45 additions & 2 deletions packages/vue-query/src/__tests__/useQuery.test-d.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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<string>()
}
})

it('should accept computed options', () => {
const options = computed(() => ({
queryKey: ['key'],
queryFn: simpleFetcher,
}))

const query = reactive(useQuery(options))

if (query.isSuccess) {
expectTypeOf(query.data).toEqualTypeOf<string>()
}
})

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<string>()
}
})
})
})
4 changes: 2 additions & 2 deletions packages/vue-query/src/types.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -12,7 +12,7 @@ type UnwrapLeaf =
| Set<any>
| WeakSet<any>

export type MaybeRef<T> = Ref<T> | T
export type MaybeRef<T> = Ref<T> | ComputedRef<T> | T

export type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T)

Expand Down

0 comments on commit f1f6592

Please sign in to comment.