Skip to content

Commit e5bfbbe

Browse files
authored
fix(types): add pageParam and direction as optional to "normal" queryFn (#7212)
* fix(types): add pageParam and direction as optional to "normal" queryFn because that's what we get when using a defaultQueryFn * chore: remove tests that are no longer true * chore: cleanup * wth?
1 parent 3cc0c15 commit e5bfbbe

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

packages/query-core/src/__tests__/query.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ describe('query', () => {
208208
expect(queryFn).toHaveBeenCalledTimes(1)
209209
const args = queryFn.mock.calls[0]![0]
210210
expect(args).toBeDefined()
211-
// @ts-expect-error page param should be undefined
212211
expect(args.pageParam).toBeUndefined()
213212
expect(args.queryKey).toEqual(key)
214213
expect(args.signal).toBeInstanceOf(AbortSignal)

packages/query-core/src/__tests__/queryClient.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'
22
import { QueryClient } from '../queryClient'
3-
import type { DataTag, InfiniteData } from '../types'
3+
import type { FetchDirection } from '../query'
4+
import type { DataTag, InfiniteData, QueryKey } from '../types'
45

56
describe('getQueryData', () => {
67
it('should be typed if key is tagged', () => {
@@ -133,3 +134,24 @@ describe('fetchInfiniteQuery', () => {
133134
})
134135
})
135136
})
137+
138+
describe('defaultOptions', () => {
139+
it('should have a typed QueryFunctionContext', () => {
140+
new QueryClient({
141+
defaultOptions: {
142+
queries: {
143+
queryFn: (context) => {
144+
expectTypeOf(context).toEqualTypeOf<{
145+
queryKey: QueryKey
146+
meta: Record<string, unknown> | undefined
147+
signal: AbortSignal
148+
pageParam?: unknown
149+
direction?: FetchDirection
150+
}>()
151+
return Promise.resolve('data')
152+
},
153+
},
154+
},
155+
})
156+
})
157+
})

packages/query-core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export type QueryFunctionContext<
6666
queryKey: TQueryKey
6767
signal: AbortSignal
6868
meta: QueryMeta | undefined
69+
pageParam?: unknown
70+
direction?: 'forward' | 'backward'
6971
}
7072
: {
7173
queryKey: TQueryKey

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, expectTypeOf, it } from 'vitest'
22
import { QueryClient } from '@tanstack/query-core'
33
import { useInfiniteQuery } from '../useInfiniteQuery'
4-
import { useQuery } from '../useQuery'
54
import type { InfiniteData } from '@tanstack/query-core'
65

76
describe('pageParam', () => {
@@ -27,26 +26,6 @@ describe('pageParam', () => {
2726
})
2827
})
2928

30-
it('there should be no pageParam passed to the queryFn of useQuery', () => {
31-
useQuery({
32-
queryKey: ['key'],
33-
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
34-
queryFn: ({ pageParam }) => {
35-
return String(pageParam)
36-
},
37-
})
38-
})
39-
40-
it('there should be no direction passed to the queryFn of useQuery', () => {
41-
useQuery({
42-
queryKey: ['key'],
43-
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
44-
queryFn: ({ direction }) => {
45-
return String(direction)
46-
},
47-
})
48-
})
49-
5029
it('initialPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
5130
const queryClient = new QueryClient()
5231
queryClient.fetchInfiniteQuery({

0 commit comments

Comments
 (0)