Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vue-query): types should now properly accept computed queryOptions #7631

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading