Skip to content

Commit 00af50a

Browse files
author
danieljayasurya-praathee
committed
fix: infer pageParam from computed queryFn with conditional skipToken
1 parent 1047cdc commit 00af50a

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

packages/vue-query/src/__tests__/useInfiniteQuery.test-d.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expectTypeOf, it } from 'vitest'
2-
import { computed, reactive } from 'vue-demi'
2+
import { computed, reactive, ref } from 'vue-demi'
33
import { sleep } from '@tanstack/query-test-utils'
4+
import { skipToken } from '..'
45
import { useInfiniteQuery } from '../useInfiniteQuery'
56
import { infiniteQueryOptions } from '../infiniteQueryOptions'
67
import type { InfiniteData } from '@tanstack/query-core'
@@ -127,4 +128,25 @@ describe('Discriminated union return type', () => {
127128
expectTypeOf(query.data).toEqualTypeOf<InfiniteData<string, unknown>>()
128129
}
129130
})
131+
132+
it('should infer pageParam from computed queryFn with conditional skipToken', () => {
133+
const enabled = ref(false)
134+
135+
useInfiniteQuery({
136+
queryKey: ['infiniteQuery', enabled],
137+
queryFn: computed(() =>
138+
enabled.value
139+
? ({ pageParam }) => {
140+
expectTypeOf(pageParam).toEqualTypeOf<number>()
141+
return sleep(0).then(() => 'Some data')
142+
}
143+
: skipToken,
144+
),
145+
initialPageParam: 1,
146+
getNextPageParam: (_lastPage, _allPages, lastPageParam) => {
147+
expectTypeOf(lastPageParam).toEqualTypeOf<number>()
148+
return lastPageParam + 1
149+
},
150+
})
151+
})
130152
})

packages/vue-query/src/useInfiniteQuery.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
InfiniteData,
1010
InfiniteQueryObserverOptions,
1111
InfiniteQueryObserverResult,
12+
QueryFunction,
1213
QueryKey,
1314
QueryObserver,
1415
} from '@tanstack/query-core'
@@ -24,6 +25,10 @@ import type {
2425
} from './types'
2526
import type { QueryClient } from './queryClient'
2627

28+
// Widen the type of the symbol to preserve contextual typing for
29+
// `computed(() => condition ? queryFn : skipToken)`.
30+
type SkipTokenForUseInfiniteQuery = symbol
31+
2732
export type UseInfiniteQueryOptions<
2833
TQueryFnData = unknown,
2934
TError = DefaultError,
@@ -48,6 +53,14 @@ export type UseInfiniteQueryOptions<
4853
TPageParam
4954
>[Property]
5055
>
56+
: Property extends 'queryFn'
57+
? MaybeRefOrGetter<
58+
QueryFunction<
59+
TQueryFnData,
60+
DeepUnwrapRef<TQueryKey>,
61+
TPageParam
62+
> | SkipTokenForUseInfiniteQuery
63+
>
5164
: MaybeRefDeep<
5265
InfiniteQueryObserverOptions<
5366
TQueryFnData,

0 commit comments

Comments
 (0)