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'
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: createKey(),
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β
38 it('should infer types for callbacks', () => {
39 infiniteQueryOptions({
40 queryKey: createKey(),
41 queryFn: () => Promise.resolve('data'),
42 staleTime: 1000,
43 getNextPageParam: () => 1,
44 initialPageParam: 1,
45 select: (data) => {
46 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
47 },
48 })
49 })
50β
51 it('should work when passed to useInfiniteQuery', () => {
52 const options = infiniteQueryOptions({
53 queryKey: createKey(),
54 queryFn: () => Promise.resolve('string'),
55 getNextPageParam: () => 1,
56 initialPageParam: 1,
57 })
58β
59 const { data } = useInfiniteQuery(options)
60β
61 // known issue: type of pageParams is unknown when returned from useInfiniteQuery
62 expectTypeOf(data).toEqualTypeOf<
63 InfiniteData<string, unknown> | undefined
64 >()
65 })
66β
67 it('should work when passed to useSuspenseInfiniteQuery', () => {
68 const options = infiniteQueryOptions({
69 queryKey: createKey(),
70 queryFn: () => Promise.resolve('string'),
71 getNextPageParam: () => 1,
72 initialPageParam: 1,
73 })
74β
75 const { data } = useSuspenseInfiniteQuery(options)
76β
77 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
78 })
79β
80 it('should work when passed to infiniteQuery', async () => {
81 const options = infiniteQueryOptions({
82 queryKey: ['key'],
83 queryFn: () => Promise.resolve('string'),
84 getNextPageParam: () => 1,
85 initialPageParam: 1,
86 })
87β
88 const data = await new QueryClient().infiniteQuery(options)
89β
90 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
91 })
92β
93 it('should work when passed to infiniteQuery with select', async () => {
94 const options = infiniteQueryOptions({
95 queryKey: ['key'],
96 queryFn: () => Promise.resolve('string'),
97 getNextPageParam: () => 1,
98 initialPageParam: 1,
99 select: (data) => data.pages,
100 })
101β
102 const data = await new QueryClient().infiniteQuery(options)
103β
104 expectTypeOf(data).toEqualTypeOf<Array<string>>()
105 })
106β
107 it('should work when passed to infiniteQuery with enabled: false', async () => {
108 const options = infiniteQueryOptions({
109 queryKey: ['key'],
110 queryFn: () => Promise.resolve('string'),
111 getNextPageParam: () => 1,
112 initialPageParam: 1,
113 enabled: false,
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 skipToken', async () => {
122 const options = infiniteQueryOptions({
123 queryKey: ['key'],
124 queryFn: skipToken,
125 getNextPageParam: () => 1,
126 initialPageParam: 1,
127 })
128β
129 const data = await new QueryClient().infiniteQuery(options)
130β
131 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, number>>()
132 })
133β
134 it('should work when passed to fetchInfiniteQuery', async () => {
135 const options = infiniteQueryOptions({
136 queryKey: createKey(),
137 queryFn: () => Promise.resolve('string'),
138 getNextPageParam: () => 1,
139 initialPageParam: 1,
140 })
141β
142 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
143 const data = await new QueryClient().fetchInfiniteQuery(options)
144β
145 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
146 })
147β
148 it('should tag the queryKey with the result type of the QueryFn', () => {
149 const { queryKey: tagged } = infiniteQueryOptions({
150 queryKey: createKey(),
151 queryFn: () => Promise.resolve('string'),
152 getNextPageParam: () => 1,
153 initialPageParam: 1,
154 })
155β
156 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
157 })
158β
159 it('should tag the queryKey even if no promise is returned', () => {
160 const { queryKey: tagged } = infiniteQueryOptions({
161 queryKey: createKey(),
162 queryFn: () => 'string',
163 getNextPageParam: () => 1,
164 initialPageParam: 1,
165 })
166β
167 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
168 })
169β
170 it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
171 const { queryKey: tagged } = infiniteQueryOptions({
172 queryKey: createKey(),
173 queryFn: () => Promise.resolve('string'),
174 select: (data) => data.pages,
175 getNextPageParam: () => 1,
176 initialPageParam: 1,
177 })
178β
179 expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
180 })
181β
182 it('should return the proper type when passed to getQueryData', () => {
183 const { queryKey: tagged } = infiniteQueryOptions({
184 queryKey: createKey(),
185 queryFn: () => Promise.resolve('string'),
186 getNextPageParam: () => 1,
187 initialPageParam: 1,
188 })
189β
190 const queryClient = new QueryClient()
191 const data = queryClient.getQueryData(tagged)
192β
193 expectTypeOf(data).toEqualTypeOf<
194 InfiniteData<string, unknown> | undefined
195 >()
196 })
197β
198 it('should properly type when passed to setQueryData', () => {
199 const { queryKey: tagged } = infiniteQueryOptions({
200 queryKey: createKey(),
201 queryFn: () => Promise.resolve('string'),
202 getNextPageParam: () => 1,
203 initialPageParam: 1,
204 })
205β
206 const queryClient = new QueryClient()
207 const data = queryClient.setQueryData(tagged, (prev) => {
208 expectTypeOf(prev).toEqualTypeOf<
209 InfiniteData<string, unknown> | undefined
210 >()
211 return prev
212 })
213β
214 expectTypeOf(data).toEqualTypeOf<
215 InfiniteData<string, unknown> | undefined
216 >()
217 })
218β
219 it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
220 const options = infiniteQueryOptions({
221 queryKey: createKey(),
222 queryFn:
223 Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
224 getNextPageParam: () => 1,
225 initialPageParam: 1,
226 })
227 // @ts-expect-error TS2345
228 const { data } = useSuspenseInfiniteQuery(options)
229 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, unknown>>()
230 })
231β
232 it('should not be allowed to be passed to non-infinite query functions', () => {
233 const queryClient = new QueryClient()
234 const options = infiniteQueryOptions({
235 queryKey: createKey(),
236 queryFn: () => Promise.resolve('string'),
237 getNextPageParam: () => 1,
238 initialPageParam: 1,
239 })
240 assertType(
241 // @ts-expect-error cannot pass infinite options to non-infinite query functions
242 useQuery(options),
243 )
244 assertType(
245 // @ts-expect-error cannot pass infinite options to non-infinite query functions
246 queryClient.query(options),
247 )
248β
249 // deprecated methods below to be removed next major version
250 assertType(
251 // @ts-expect-error cannot pass infinite options to non-infinite query functions
252 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
253 queryClient.ensureQueryData(options),
254 )
255 assertType(
256 // @ts-expect-error cannot pass infinite options to non-infinite query functions
257 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
258 queryClient.fetchQuery(options),
259 )
260 assertType(
261 // @ts-expect-error cannot pass infinite options to non-infinite query functions
262 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
263 queryClient.prefetchQuery(options),
264 )
265 })
266β
267 it('should allow optional initialData function', () => {
268 const initialData: { example: boolean } | undefined = { example: true }
269 const queryOptions = infiniteQueryOptions({
270 queryKey: createKey(),
271 queryFn: () => initialData,
272 initialData: initialData
273 ? () => ({ pages: [initialData], pageParams: [] })
274 : undefined,
275 getNextPageParam: () => 1,
276 initialPageParam: 1,
277 })
278 expectTypeOf(queryOptions.initialData).toExtend<
279 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
280 | InfiniteData<{ example: boolean }, number>
281 | undefined
282 >()
283 })
284β
285 it('should allow optional initialData object', () => {
286 const initialData: { example: boolean } | undefined = { example: true }
287 const queryOptions = infiniteQueryOptions({
288 queryKey: createKey(),
289 queryFn: () => initialData,
290 initialData: initialData
291 ? { pages: [initialData], pageParams: [] }
292 : undefined,
293 getNextPageParam: () => 1,
294 initialPageParam: 1,
295 })
296 expectTypeOf(queryOptions.initialData).toExtend<
297 | InitialDataFunction<InfiniteData<{ example: boolean }, number>>
298 | InfiniteData<{ example: boolean }, number>
299 | undefined
300 >()
301 })
302β
303 it('should keep data possibly undefined when initialData is a ternary that can be undefined', () => {
304 const options = infiniteQueryOptions({
305 queryKey: createKey(),
306 queryFn: () => Promise.resolve({ example: true }),
307 initialData:
308 Math.random() > 0.5
309 ? { pages: [{ example: true }], pageParams: [1] }
310 : undefined,
311 getNextPageParam: () => 1,
312 initialPageParam: 1,
313 })
314β
315 const { data } = useInfiniteQuery(options)
316 expectTypeOf(data).toEqualTypeOf<
317 InfiniteData<{ example: boolean }, unknown> | undefined
318 >()
319 })
320β
321 it('should return a custom query key type', () => {
322 type MyQueryKey = [Array<string>, { type: 'foo' }]
323β
324 const options = infiniteQueryOptions({
325 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
326 queryFn: () => Promise.resolve(1),
327 getNextPageParam: () => 1,
328 initialPageParam: 1,
329 })
330β
331 expectTypeOf(options.queryKey).toEqualTypeOf<
332 DataTag<MyQueryKey, InfiniteData<number>, Error>
333 >()
334 })
335β
336 it('should return a custom query key type with datatag', () => {
337 type MyQueryKey = DataTag<
338 [Array<string>, { type: 'foo' }],
339 number,
340 Error & { myMessage: string }
341 >
342β
343 const options = infiniteQueryOptions({
344 queryKey: [['key'], { type: 'foo' }] as MyQueryKey,
345 queryFn: () => Promise.resolve(1),
346 getNextPageParam: () => 1,
347 initialPageParam: 1,
348 })
349β
350 expectTypeOf(options.queryKey).toEqualTypeOf<
351 DataTag<MyQueryKey, InfiniteData<number>, Error & { myMessage: string }>
352 >()
353 })
354})