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