Skip to content

Commit dccf5b6

Browse files
test(vue-query): pin suspense() Promise<TResult> parameterization
Adds a regression test asserting that `useQuery(...).suspense()` resolves to `Promise<QueryObserverResult<TData, TError>>` (parameterized by `TResult` on `UseBaseQueryReturnType`) and that awaiting it preserves the discriminated union narrowing on `isSuccess` / `isError`. Closes the verification gap noted on #10580 — the parameterization was previously only validated via tsc build, not by an explicit `expectTypeOf` assertion.
1 parent a8b5368 commit dccf5b6

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

packages/vue-query/src/__tests__/useQuery.test-d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { computed, reactive, ref } from 'vue-demi'
33
import { queryKey, sleep } from '@tanstack/query-test-utils'
44
import { queryOptions, useQuery } from '..'
55
import type { Ref } from 'vue-demi'
6+
import type { QueryObserverResult } from '@tanstack/query-core'
67
import type { OmitKeyof, UseQueryOptions } from '..'
78

89
describe('useQuery', () => {
@@ -336,6 +337,30 @@ describe('useQuery', () => {
336337
expectTypeOf(data).toEqualTypeOf<string>()
337338
}
338339
})
340+
341+
it('suspense() returns Promise<QueryObserverResult> parameterized by TResult', async () => {
342+
const key = queryKey()
343+
344+
const query = useQuery({
345+
queryKey: key,
346+
queryFn: () => sleep(0).then(() => 'Some data'),
347+
})
348+
349+
// Pinning the resolved type guards the `suspense: () => Promise<TResult>`
350+
// parameterization on `UseBaseQueryReturnType` against accidental
351+
// collapse to `Promise<unknown>` or `Promise<QueryObserverResult<unknown, unknown>>`.
352+
expectTypeOf(query.suspense()).toEqualTypeOf<
353+
Promise<QueryObserverResult<string, Error>>
354+
>()
355+
356+
const result = await query.suspense()
357+
// Awaiting the promise must preserve the discriminated union so
358+
// narrowing on `isSuccess` reduces `data` to the queryFn return type.
359+
if (result.isSuccess) {
360+
expectTypeOf(result.data).toEqualTypeOf<string>()
361+
expectTypeOf(result.error).toEqualTypeOf<null>()
362+
}
363+
})
339364
})
340365

341366
describe('accept ref options', () => {

0 commit comments

Comments
 (0)