Open History to see the last commit
1import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
2import { queryKey } from '@tanstack/query-test-utils'
3import { assertType, describe, expectTypeOf, it } from 'vitest'
4β
5import { infiniteQueryOptions } from '../infiniteQueryOptions'
6import { useInfiniteQuery } from '../useInfiniteQuery'
7import { useQuery } from '../useQuery'
8import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
9import type {
10 DataTag,
11 InfiniteData,
12 InitialDataFunction,
13} from '@tanstack/query-core'
14β
15// Regression test for exported infiniteQueryOptions inference under declaration emit.
16// TypeScript should be able to name the return type without expanding the
17// internal data tag symbols into the consumer's .d.ts output.
18export const exportedInfiniteQueryOptions = infiniteQueryOptions({
19 queryKey: ['invalid'],
20 getNextPageParam: () => 1,
21 initialPageParam: 1,
22})
23β
24describe('infiniteQueryOptions', () => {
25 it('should not allow excess properties', () => {
26 assertType(
27 infiniteQueryOptions({
28 queryKey: queryKey(),
29 queryFn: () => Promise.resolve('data'),
30 getNextPageParam: () => 1,
31 initialPageParam: 1,
32 // @ts-expect-error this is a good error, because stallTime does not exist!
33 stallTime: 1000,
34 }),
35 )
36 })
37 it('should infer types for callbacks', () => {
38 infiniteQueryOptions({
39 queryKey: queryKey(),
40 queryFn: () => Promise.resolve('data'),
41 staleTime: 1000,
42 getNextPageParam: () => 1,
43 initialPageParam: 1,
44 select: (data) => {
45 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
46 },
47 })
48 })
49 it('should work when passed to useInfiniteQuery', () => {
50 const options = infiniteQueryOptions({
51 queryKey: queryKey(),
52 queryFn: () => Promise.resolve('string'),
53 getNextPageParam: () => 1,
54 initialPageParam: 1,
55 })
56β
57 const { data } = useInfiniteQuery(options)
58β
59 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
60 expectTypeOf(data).toEqualTypeOf<
61 InfiniteData<string, unknown> | undefined
62 >()
63 })
64 it('should work when passed to useSuspenseInfiniteQuery', () => {
65 const options = infiniteQueryOptions({
66 queryKey: queryKey(),
67 queryFn: () => Promise.resolve('string'),
68 getNextPageParam: () => 1,
69 initialPageParam: 1,
70 })
71β
72 const { data } = useSuspenseInfiniteQuery(options)
73β
74 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
75 })
76 it('should work when passed to infiniteQuery', async () => {
77 const options = infiniteQueryOptions({
78 queryKey: ['key'],
79 queryFn: () => Promise.resolve('string'),
80 getNextPageParam: () => 1,
81 initialPageParam: 1,
82 })
83β
84 const data = await new QueryClient().infiniteQuery(options)
85β
86 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
87 })
88 it('should work when passed to infiniteQuery with select', async () => {
89 const options = infiniteQueryOptions({
90 queryKey: ['key'],
91 queryFn: () => Promise.resolve('string'),
92 getNextPageParam: () => 1,
93 initialPageParam: 1,
94 select: (data) => data.pages,
95 })
96β
97 const data = await new QueryClient().infiniteQuery(options)
98β
99 expectTypeOf(data).toEqualTypeOf<Array<string>>()
100 })
101 it('should work when passed to infiniteQuery with enabled: false', async () => {
102 const options = infiniteQueryOptions({
103 queryKey: ['key'],
104 queryFn: () => Promise.resolve('string'),
105 getNextPageParam: () => 1,
106 initialPageParam: 1,
107 enabled: false,
108 })
109β
110 const data = await new QueryClient().infiniteQuery(options)
111β
112 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
113 })
114 it('should work when passed to infiniteQuery with skipToken', async () => {
115 const options = infiniteQueryOptions({
116 queryKey: ['key'],
117 queryFn: skipToken,
118 getNextPageParam: () => 1,
119 initialPageParam: 1,
120 })
121β
122 const data = await new QueryClient().infiniteQuery(options)
123β
124 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
125 })
126 it('should work when passed to fetchInfiniteQuery', async () => {
127 const options = infiniteQueryOptions({
128 queryKey: queryKey(),
129 queryFn: () => Promise.resolve('string'),
130 getNextPageParam: () => 1,
131 initialPageParam: 1,
132 })
133β
134 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
135 const data = await new QueryClient().fetchInfiniteQuery(options)
136β
137 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
138 })
139 it('should tag the queryKey with the result type of the QueryFn', () => {
140 const { queryKey: tagged } = infiniteQueryOptions({
141 queryKey: queryKey(),
142 queryFn: () => Promise.resolve('string'),
143 getNextPageParam: () => 1,
144 initialPageParam: 1,
145 })
146β
147 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
148 })
149 it('should tag the queryKey even if no promise is returned', () => {
150 const { queryKey: tagged } = infiniteQueryOptions({
151 queryKey: queryKey(),
152 queryFn: () => 'string',
153 getNextPageParam: () => 1,
154 initialPageParam: 1,
155 })
156β
157 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
158 })
159 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
160 const { queryKey: tagged } = infiniteQueryOptions({
161 queryKey: queryKey(),
162 queryFn: () => Promise.resolve('string'),
163 select: (data) => data.pages,
164 getNextPageParam: () => 1,
165 initialPageParam: 1,
166 })
167β
168 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
169 })
170 it('should return the proper type when passed to getQueryData', () => {
171 const { queryKey: tagged } = infiniteQueryOptions({
172 queryKey: queryKey(),
173 queryFn: () => Promise.resolve('string'),
174 getNextPageParam: () => 1,
175 initialPageParam: 1,
176 })
177β
178 const queryClient = new QueryClient()
179 const data = queryClient.getQueryData(tagged)
180β
181 expectTypeOf(data).toEqualTypeOf<
182 InfiniteData<string, unknown> | undefined
183 >()
184 })
185 it('should properly type when passed to setQueryData', () => {
186 const { queryKey: tagged } = infiniteQueryOptions({
187 queryKey: queryKey(),
188 queryFn: () => Promise.resolve('string'),
189 getNextPageParam: () => 1,
190 initialPageParam: 1,
191 })
192β
193 const queryClient = new QueryClient()
194 const data = queryClient.setQueryData(tagged, (prev) => {
195 expectTypeOf(prev).toEqualTypeOf<
196 InfiniteData<string, unknown> | undefined
197 >()
198 return prev
199 })
200β
201 expectTypeOf(data).toEqualTypeOf<
202 InfiniteData<string, unknown> | undefined
203 >()
204 })
205 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
206 const options = infiniteQueryOptions({
207 queryKey: queryKey(),
208 queryFn:
209 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
210 getNextPageParam: () => 1,
211 initialPageParam: 1,
212 })
213 // @ts-expect-error TS2345
214 const { data } = useSuspenseInfiniteQuery(options)
215 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
216 })
217β
218 it('should not be allowed to be passed to non-infinite query functions', () => {
219 const queryClient = new QueryClient()
220 const options = infiniteQueryOptions({
221 queryKey: queryKey(),
222 queryFn: () => Promise.resolve('string'),
223 getNextPageParam: () => 1,
224 initialPageParam: 1,
225 })
226 assertType(
227 // @ts-expect-error cannot pass infinite options to non-infinite query functions
228 useQuery(options),
229 )
230 assertType(
231 // @ts-expect-error cannot pass infinite options to non-infinite query functions
232 queryClient.query(options),
233 )
234β
235 // deprecated methods below to be removed next major version
236 assertType(
237 // @ts-expect-error cannot pass infinite options to non-infinite query functions
238 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
239 queryClient.ensureQueryData(options),
240 )
241 assertType(
242 // @ts-expect-error cannot pass infinite options to non-infinite query functions
243 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
244 queryClient.fetchQuery(options),
245 )
246 assertType(
247 // @ts-expect-error cannot pass infinite options to non-infinite query functions
248 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
249 queryClient.prefetchQuery(options),
250 )
251 })
252β
253 it('should allow optional initialData function', () => {
254 const initialData: { example: boolean } | undefined = { example: true }
255 const queryOptions = infiniteQueryOptions({
256 queryKey: queryKey(),
257 queryFn: () => initialData,
258 initialData: initialData
259 ? () => ({ pages: [initialData], pageParams: [] })
260 : undefined,
261 getNextPageParam: () => 1,
262 initialPageParam: 1,
263 })
264 expectTypeOf(queryOptions.initialData).toExtend<
265 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
266 | InfiniteData<{ example: boolean }, number>
267 | undefined
268 >()
269 })
270β
271 it('should allow optional initialData object', () => {
272 const initialData: { example: boolean } | undefined = { example: true }
273 const queryOptions = infiniteQueryOptions({
274 queryKey: queryKey(),
275 queryFn: () => initialData,
276 initialData: initialData
277 ? { pages: [initialData], pageParams: [] }
278 : undefined,
279 getNextPageParam: () => 1,
280 initialPageParam: 1,
281 })
282 expectTypeOf(queryOptions.initialData).toExtend<
283 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
284 | InfiniteData<{ example: boolean }, number>
285 | undefined
286 >()
287 })
288β
289 it('should return a custom query key type', () => {
290 type MyQueryKey = [Array<string>, { type: 'foo' }]
291β
292 const options = infiniteQueryOptions({
293 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
294 queryFn: () => Promise.resolve(1),
295 getNextPageParam: () => 1,
296 initialPageParam: 1,
297 })
298β
299 expectTypeOf(options.queryKey).toEqualTypeOf<
300 DataTag<MyQueryKey, InfiniteData<number>, Error>
301 >()
302 })
303β
304 it('should return a custom query key type with datatag', () => {
305 type MyQueryKey = DataTag<
306 [Array<string>, { type: 'foo' }],
307 number,
308 Error & { myMessage: string }
309 >
310β
311 const options = infiniteQueryOptions({
312 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
313 queryFn: () => Promise.resolve(1),
314 getNextPageParam: () => 1,
315 initialPageParam: 1,
316 })
317β
318 expectTypeOf(options.queryKey).toEqualTypeOf<
319 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
320 >()
321 })
322})