Skip to content

Commit 268813a

Browse files
committed
add some failing tests to show the problem with pageParam typings
1 parent f2616e2 commit 268813a

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { useInfiniteQuery } from '../useInfiniteQuery'
2+
import { useQuery } from '../useQuery'
3+
import type { Expect, Equal } from './utils'
4+
import { doNotExecute } from './utils'
5+
import { QueryClient } from '@tanstack/query-core'
6+
7+
describe('pageParam', () => {
8+
it('defaultPageParam should define type of param passed to queryFunctionContext', () => {
9+
doNotExecute(() => {
10+
useInfiniteQuery({
11+
queryKey: ['key'],
12+
queryFn: ({ pageParam }) => {
13+
const result: Expect<Equal<number, typeof pageParam>> = true
14+
return result
15+
},
16+
defaultPageParam: 1,
17+
getNextPageParam: () => undefined,
18+
})
19+
})
20+
})
21+
22+
it('there should be no pageParam passed to queryFn of useQuery', () => {
23+
doNotExecute(() => {
24+
useQuery({
25+
queryKey: ['key'],
26+
// @ts-expect-error there should be no pageParam passed to queryFn of useQuery
27+
queryFn: ({ pageParam }) => {
28+
return String(pageParam)
29+
},
30+
})
31+
})
32+
})
33+
34+
it('defaultPageParam should define type of param passed to queryFunctionContext for fetchInfiniteQuery', () => {
35+
doNotExecute(() => {
36+
const queryClient = new QueryClient()
37+
queryClient.fetchInfiniteQuery({
38+
queryKey: ['key'],
39+
queryFn: ({ pageParam }) => {
40+
const result: Expect<Equal<number, typeof pageParam>> = true
41+
return result
42+
},
43+
defaultPageParam: 1,
44+
})
45+
})
46+
})
47+
48+
it('defaultPageParam should define type of param passed to queryFunctionContext for prefetchInfiniteQuery', () => {
49+
doNotExecute(() => {
50+
const queryClient = new QueryClient()
51+
queryClient.prefetchInfiniteQuery({
52+
queryKey: ['key'],
53+
queryFn: ({ pageParam }) => {
54+
const result: Expect<Equal<number, typeof pageParam>> = true
55+
return result
56+
},
57+
defaultPageParam: 1,
58+
})
59+
})
60+
})
61+
})

packages/react-query/src/__tests__/useQuery.types.test.tsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { useQuery } from '../useQuery'
2-
3-
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
4-
T,
5-
>() => T extends Y ? 1 : 2
6-
? true
7-
: false
8-
9-
export type Expect<T extends true> = T
10-
11-
const doNotExecute = (_func: () => void) => true
2+
import type { Expect, Equal } from './utils'
3+
import { doNotExecute } from './utils'
124

135
describe('initialData', () => {
146
describe('Config object overload', () => {

packages/react-query/src/__tests__/utils.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ export function expectType<T>(_: T): void {
7979
return undefined
8080
}
8181

82+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <
83+
T,
84+
>() => T extends Y ? 1 : 2
85+
? true
86+
: false
87+
88+
export type Expect<T extends true> = T
89+
8290
/**
8391
* Assert the parameter is not typed as `any`
8492
*/
@@ -100,3 +108,5 @@ export function setIsServer(isServer: boolean) {
100108
})
101109
}
102110
}
111+
112+
export const doNotExecute = (_func: () => void) => true

0 commit comments

Comments
 (0)