Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryClient.test-d.tsx

1,475 lines47.5 KB

1import { assertType, describe, expectTypeOf, it } from 'vitest'
2import { queryKey } from '@tanstack/query-test-utils'
3import { QueryClient } from '../queryClient'
4import { skipToken } from '../utils'
5import type { MutationFilters, QueryFilters, Updater } from '../utils'
6import type { Mutation } from '../mutation'
7import type { Query, QueryState } from '../query'
8import type { QueryCache } from '../queryCache'
9import type { MutationCache } from '../mutationCache'
10import type {
11 DataTag,
12 DefaultError,
13 DefaultOptions,
14 DefaultedQueryObserverOptions,
15 EnsureQueryDataOptions,
16 FetchInfiniteQueryOptions,
17 InfiniteData,
18 InfiniteQueryExecuteOptions,
19 MutationFunctionContext,
20 MutationKey,
21 MutationObserverOptions,
22 MutationOptions,
23 OmitKeyof,
24 QueryClientConfig,
25 QueryKey,
26 QueryObserverOptions,
27 SetDataOptions,
28 UnsetMarker,
29} from '../types'
30​
31type TypedData = { foo: string }
32type TypedError = DefaultError & { bar: string }
33​
34const typedInfiniteQueryOptions: InfiniteQueryExecuteOptions<
35 TypedData,
36 TypedError,
37 InfiniteData<TypedData>
38> = {
39 queryKey: ['key', 'infinite'],
40 pages: 5,
41 getNextPageParam: (lastPage) => {
42 expectTypeOf(lastPage).toEqualTypeOf<TypedData>()
43 return 0
44 },
45 initialPageParam: 0,
47​
48const typedQueryOptions: EnsureQueryDataOptions<TypedData, TypedError> = {
49 queryKey: ['key', 'query'],
51​
52const typedFetchInfiniteQueryOptions: FetchInfiniteQueryOptions<
53 TypedData,
54 TypedError
55> = {
56 queryKey: ['key', 'infinite'],
57 pages: 5,
58 getNextPageParam: (lastPage) => {
59 expectTypeOf(lastPage).toEqualTypeOf<TypedData>()
60 return 0
61 },
62 initialPageParam: 0,
64​
65const typedMutationOptions: MutationOptions<TypedData, TypedError> = {}
66​
67const typedQueryFilters: QueryFilters<
68 DataTag<QueryKey, TypedData, TypedError>
69> = {}
70​
71const typedMutationFilters: MutationFilters<TypedData, TypedError> = {}
72​
73const typedFilterKey = typedQueryFilters.queryKey!
74const typedMutationKey = typedMutationOptions.mutationKey!
75​
76const untypedQueryOptions: EnsureQueryDataOptions = {
77 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
78 queryKey: ['key'] as any,
80​
81const untypedFetchInfiniteQueryOptions: FetchInfiniteQueryOptions = {
82 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
83 queryKey: ['key'] as any,
84 pages: 5,
85 getNextPageParam: (lastPage) => {
86 expectTypeOf(lastPage).toEqualTypeOf<unknown>()
87 return 0
88 },
89 initialPageParam: 0,
91​
92const untypedMutationOptions: MutationOptions = {}
93const untypedQueryFilters: QueryFilters = {}
94const untypedMutationFilters: MutationFilters = {}
95const untypedFilterKey = untypedQueryFilters.queryKey!
96const untypedMutationKey = untypedMutationOptions.mutationKey!
97​
98type SuccessCallback = () => unknown
99​
100describe('queryClient', () => {
101 describe('getQueryData', () => {
102 it('should be typed if key is tagged', () => {
103 const key = ['key'] as DataTag<Array<string>, number>
104 const queryClient = new QueryClient()
105 const data = queryClient.getQueryData(key)
106​
107 expectTypeOf(data).toEqualTypeOf<number | undefined>()
108 })
109​
110 it('should infer unknown if key is not tagged', () => {
111 const key = ['key'] as const
112 const queryClient = new QueryClient()
113 const data = queryClient.getQueryData(key)
114​
115 expectTypeOf(data).toEqualTypeOf<unknown>()
116 })
117​
118 it('should infer passed generic if passed', () => {
119 const key = ['key'] as const
120 const queryClient = new QueryClient()
121 const data = queryClient.getQueryData<number>(key)
122​
123 expectTypeOf(data).toEqualTypeOf<number | undefined>()
124 })
125​
126 it('should only allow Arrays to be passed', () => {
127 assertType<Parameters<QueryClient['getQueryData']>>([
128 // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
129 { queryKey: 'key' },
130 ])
131 })
132​
133 it('should resolve the data type from a tagged filter key', () => {
134 const queryClient = new QueryClient()
135 const data = queryClient.getQueryData(typedFilterKey)
136​
137 expectTypeOf(data).toEqualTypeOf<TypedData | undefined>()
138 })
139​
140 it('should resolve unknown from an untyped filter key', () => {
141 const queryClient = new QueryClient()
142 const data = queryClient.getQueryData(untypedFilterKey)
143​
144 expectTypeOf(data).toEqualTypeOf<unknown>()
145 })
146 })
147​
148 describe('setQueryData', () => {
149 it('should type the updater if the key is tagged', () => {
150 const key = ['key'] as DataTag<Array<string>, number>
151 const queryClient = new QueryClient()
152 const data = queryClient.setQueryData(key, (prev) => {
153 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
154 return prev
155 })
156​
157 expectTypeOf(data).toEqualTypeOf<number | undefined>()
158 })
159​
160 it('should type the value if the key is tagged', () => {
161 const key = ['key'] as DataTag<Array<string>, number>
162 const queryClient = new QueryClient()
163​
164 // @ts-expect-error value should be a number
165 queryClient.setQueryData(key, '1')
166​
167 // @ts-expect-error value should be a number
168 queryClient.setQueryData(key, () => '1')
169​
170 const data = queryClient.setQueryData(key, 1)
171​
172 expectTypeOf(data).toEqualTypeOf<number | undefined>()
173 })
174​
175 it('should infer unknown for updater if key is not tagged', () => {
176 const key = ['key'] as const
177 const queryClient = new QueryClient()
178 const data = queryClient.setQueryData(key, (prev) => {
179 expectTypeOf(prev).toEqualTypeOf<unknown>()
180 return prev
181 })
182​
183 expectTypeOf(data).toEqualTypeOf<unknown>()
184 })
185​
186 it('should infer unknown for value if key is not tagged', () => {
187 const key = ['key'] as const
188 const queryClient = new QueryClient()
189 const data = queryClient.setQueryData(key, 'foo')
190​
191 expectTypeOf(data).toEqualTypeOf<unknown>()
192 })
193​
194 it('should infer passed generic if passed', () => {
195 const key = ['key'] as const
196 const queryClient = new QueryClient()
197 const data = queryClient.setQueryData<string>(key, (prev) => {
198 expectTypeOf(prev).toEqualTypeOf<string | undefined>()
199 return prev
200 })
201​
202 expectTypeOf(data).toEqualTypeOf<string | undefined>()
203 })
204​
205 it('should infer passed generic for value', () => {
206 const key = ['key'] as const
207 const queryClient = new QueryClient()
208 const data = queryClient.setQueryData<string>(key, 'foo')
209​
210 expectTypeOf(data).toEqualTypeOf<string | undefined>()
211 })
212​
213 it('should preserve updater parameter type inference when used in functions with explicit return types', () => {
214 const key = ['key'] as DataTag<Array<string>, number>
215 const queryClient = new QueryClient()
216​
217 // Simulate usage inside a function with explicit return type
218 // The outer function returns 'unknown' but this shouldn't affect the updater's type inference
219 ;(() =>
220 queryClient.setQueryData(key, (data) => {
221 expectTypeOf(data).toEqualTypeOf<number | undefined>()
222 return data
223 })) satisfies () => unknown
224 })
225​
226 it('should infer the updater parameter inside an expression body arrow function', () => {
227 const key = ['key'] as DataTag<Array<string>, number>
228 const queryClient = new QueryClient()
229​
230 const callbackTest: SuccessCallback = () =>
231 queryClient.setQueryData(key, (data) => {
232 expectTypeOf(data).toEqualTypeOf<number | undefined>()
233 return data
234 })
235 expectTypeOf(callbackTest).toEqualTypeOf<SuccessCallback>()
236 })
237​
238 it('should infer the updater parameter inside a block body arrow function', () => {
239 const key = ['key'] as DataTag<Array<string>, number>
240 const queryClient = new QueryClient()
241​
242 const callbackTest2: SuccessCallback = () => {
243 queryClient.setQueryData(key, (data) => {
244 expectTypeOf(data).toEqualTypeOf<number | undefined>()
245 return data
246 })
247 }
248 expectTypeOf(callbackTest2).toEqualTypeOf<SuccessCallback>()
249 })
250​
251 it('should accept an optional SetDataOptions third argument', () => {
252 const key = ['key'] as DataTag<Array<string>, number>
253 const queryClient = new QueryClient()
254​
255 queryClient.setQueryData(key, 1)
256 queryClient.setQueryData(key, 1, { updatedAt: 0 })
257​
258 type SetDataOptionsArg = Parameters<
259 typeof queryClient.setQueryData<number, typeof key>
260 >[2]
261 expectTypeOf<SetDataOptionsArg>().toEqualTypeOf<
262 SetDataOptions | undefined
263 >()
264 expectTypeOf<NonNullable<SetDataOptionsArg>['updatedAt']>().toEqualTypeOf<
265 number | undefined
266 >()
267 })
268​
269 it('should resolve the data type from a tagged filter key', () => {
270 const queryClient = new QueryClient()
271​
272 // Type the value before passing it: TypeScript 5.4's `NoInfer` can't match
273 // an inline object literal against the value branch of the `Updater` union
274 // here, so it falls back to the function branch and reports the literal as
275 // excess properties. Annotating sidesteps that (TS >= 5.5 handles it).
276 const newData: TypedData = { foo: '' }
277 const data = queryClient.setQueryData(typedFilterKey, newData)
278​
279 expectTypeOf(data).toEqualTypeOf<TypedData | undefined>()
280 expectTypeOf<
281 Parameters<
282 typeof queryClient.setQueryData<unknown, typeof typedFilterKey>
283 >[1]
284 >().toEqualTypeOf<Updater<TypedData | undefined, TypedData | undefined>>()
285 })
286​
287 it('should resolve unknown from an untyped filter key', () => {
288 const queryClient = new QueryClient()
289 const data = queryClient.setQueryData(untypedFilterKey, { foo: '' })
290​
291 expectTypeOf(data).toEqualTypeOf<unknown>()
292 expectTypeOf<
293 Parameters<
294 typeof queryClient.setQueryData<unknown, typeof untypedFilterKey>
295 >[1]
296 >().toEqualTypeOf<Updater<unknown, unknown>>()
297 })
298 })
299​
300 describe('setQueriesData', () => {
301 it('should resolve QueryKey and unknown for a tagged filter', () => {
302 const queryClient = new QueryClient()
303 // TODO: types here are wrong and coming up undefined
304 const data = queryClient.setQueriesData(typedQueryFilters, { foo: '' })
305​
306 expectTypeOf(data).toEqualTypeOf<Array<[QueryKey, unknown]>>()
307 expectTypeOf<
308 Parameters<
309 typeof queryClient.setQueriesData<unknown, typeof typedQueryFilters>
310 >[1]
311 >().toEqualTypeOf<Updater<unknown, unknown>>()
312 })
313​
314 it('should resolve QueryKey and unknown for an untyped filter', () => {
315 const queryClient = new QueryClient()
316 // TODO: types here are wrong and coming up undefined
317 const data = queryClient.setQueriesData(untypedQueryFilters, { foo: '' })
318​
319 expectTypeOf(data).toEqualTypeOf<Array<[QueryKey, unknown]>>()
320 expectTypeOf<
321 Parameters<
322 typeof queryClient.setQueriesData<unknown, typeof untypedQueryFilters>
323 >[1]
324 >().toEqualTypeOf<Updater<unknown, unknown>>()
325 })
326 })
327​
328 describe('getQueryState', () => {
329 it('should be loose typed without tag', () => {
330 const key = ['key'] as const
331 const queryClient = new QueryClient()
332 const data = queryClient.getQueryState(key)
333​
334 expectTypeOf(data).toEqualTypeOf<QueryState<unknown, Error> | undefined>()
335 })
336​
337 it('should be typed if key is tagged', () => {
338 const key = ['key'] as DataTag<Array<string>, number>
339 const queryClient = new QueryClient()
340 const data = queryClient.getQueryState(key)
341​
342 expectTypeOf(data).toEqualTypeOf<QueryState<number, Error> | undefined>()
343 })
344​
345 it('should be typed including error if key is tagged', () => {
346 type CustomError = Error & { customError: string }
347 const key = ['key'] as DataTag<Array<string>, number, CustomError>
348 const queryClient = new QueryClient()
349 const data = queryClient.getQueryState(key)
350​
351 expectTypeOf(data).toEqualTypeOf<
352 QueryState<number, CustomError> | undefined
353 >()
354 })
355​
356 it('should resolve the data and error from a tagged filter key', () => {
357 const queryClient = new QueryClient()
358 const state = queryClient.getQueryState(typedFilterKey)
359​
360 expectTypeOf(state).toEqualTypeOf<
361 QueryState<TypedData, TypedError> | undefined
362 >()
363 })
364​
365 it('should resolve unknown and DefaultError from an untyped filter key', () => {
366 const queryClient = new QueryClient()
367 const state = queryClient.getQueryState(untypedFilterKey)
368​
369 expectTypeOf(state).toEqualTypeOf<
370 QueryState<unknown, DefaultError> | undefined
371 >()
372 })
373 })
374​
375 describe('fetchQuery', () => {
376 it('should not allow passing select option', () => {
377 assertType<Parameters<QueryClient['fetchQuery']>>([
378 {
379 queryKey: ['key'],
380 queryFn: () => Promise.resolve('string'),
381 // @ts-expect-error `select` is not supported on fetchQuery options
382 select: (data: string) => data.length,
383 },
384 ])
385 })
386​
387 it('should resolve with the data type', async () => {
388 const queryClient = new QueryClient()
389 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
390 const data = await queryClient.fetchQuery(typedQueryOptions)
391​
392 expectTypeOf(data).toEqualTypeOf<TypedData>()
393 })
394​
395 it('should resolve with unknown when untyped', async () => {
396 const queryClient = new QueryClient()
397 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
398 const data = await queryClient.fetchQuery(untypedQueryOptions)
399​
400 expectTypeOf(data).toEqualTypeOf<unknown>()
401 })
402 })
403​
404 describe('fetchInfiniteQuery', () => {
405 it('should not allow passing select option', () => {
406 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
407 {
408 queryKey: ['key'],
409 queryFn: () => Promise.resolve({ count: 1 }),
410 initialPageParam: 1,
411 getNextPageParam: () => 2,
412 // @ts-expect-error `select` is not supported on fetchInfiniteQuery options
413 select: (data: InfiniteData<{ count: number }, number>) => ({
414 pages: data.pages.map((x) => `count: ${x.count}`),
415 pageParams: data.pageParams,
416 }),
417 },
418 ])
419 })
420​
421 it('should allow passing pages', async () => {
422 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
423 const data = await new QueryClient().fetchInfiniteQuery({
424 queryKey: queryKey(),
425 queryFn: () => Promise.resolve('string'),
426 getNextPageParam: () => 1,
427 initialPageParam: 1,
428 pages: 5,
429 })
430​
431 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
432 })
433​
434 it('should allow passing getNextPageParam without pages', () => {
435 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
436 {
437 queryKey: ['key'],
438 queryFn: () => Promise.resolve('string'),
439 initialPageParam: 1,
440 getNextPageParam: () => 1,
441 },
442 ])
443 })
444​
445 it('should not allow passing pages without getNextPageParam', () => {
446 assertType<Parameters<QueryClient['fetchInfiniteQuery']>>([
447 // @ts-expect-error Property 'getNextPageParam' is missing
448 {
449 queryKey: ['key'],
450 queryFn: () => Promise.resolve('string'),
451 initialPageParam: 1,
452 pages: 5,
453 },
454 ])
455 })
456​
457 it('should resolve with InfiniteData of the data type', async () => {
458 const queryClient = new QueryClient()
459 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
460 const data = await queryClient.fetchInfiniteQuery(
461 typedFetchInfiniteQueryOptions,
462 )
463​
464 expectTypeOf(data).toEqualTypeOf<InfiniteData<TypedData, unknown>>()
465 })
466​
467 it('should resolve with InfiniteData of unknown when untyped', async () => {
468 const queryClient = new QueryClient()
469 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
470 const data = await queryClient.fetchInfiniteQuery(
471 untypedFetchInfiniteQueryOptions,
472 )
473​
474 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, unknown>>()
475 })
476 })
477​
478 describe('query', () => {
479 it('should allow passing select option', () => {
480 const result = new QueryClient().query({
481 queryKey: ['key'],
482 queryFn: () => Promise.resolve('string'),
483 select: (data) => data.length,
484 })
485​
486 expectTypeOf(result).toEqualTypeOf<Promise<number>>()
487 })
488​
489 it('should infer select type with skipToken queryFn', () => {
490 const result = new QueryClient().query({
491 queryKey: ['key'],
492 queryFn: skipToken,
493 select: (data: string) => data.length,
494 })
495​
496 expectTypeOf(result).toEqualTypeOf<Promise<number>>()
497 })
498​
499 it('should not allow enabled', () => {
500 assertType<Parameters<QueryClient['query']>>([
501 {
502 queryKey: ['key'],
503 queryFn: skipToken,
504 // @ts-expect-error enabled is not supported for imperative queries
505 enabled: false,
506 },
507 ])
508 })
509​
510 it('should resolve with the data type', async () => {
511 const queryClient = new QueryClient()
512 const data = await queryClient.query(typedQueryOptions)
513​
514 expectTypeOf(data).toEqualTypeOf<TypedData>()
515 })
516​
517 it('should resolve with unknown when untyped', async () => {
518 const queryClient = new QueryClient()
519 const data = await queryClient.query(untypedQueryOptions)
520​
521 expectTypeOf(data).toEqualTypeOf<unknown>()
522 })
523 })
524​
525 describe('ensureQueryData', () => {
526 it('should resolve with the data type', async () => {
527 const queryClient = new QueryClient()
528 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
529 const data = await queryClient.ensureQueryData(typedQueryOptions)
530​
531 expectTypeOf(data).toEqualTypeOf<TypedData>()
532 })
533​
534 it('should resolve with unknown when untyped', async () => {
535 const queryClient = new QueryClient()
536 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
537 const data = await queryClient.ensureQueryData(untypedQueryOptions)
538​
539 expectTypeOf(data).toEqualTypeOf<unknown>()
540 })
541 })
542​
543 describe('infiniteQuery', () => {
544 it('should allow passing select option', () => {
545 const result = new QueryClient().infiniteQuery({
546 queryKey: ['key'],
547 queryFn: () => Promise.resolve({ count: 1 }),
548 initialPageParam: 1,
549 getNextPageParam: () => 2,
550 select: (data) => ({
551 pages: data.pages.map(
552 // oxlint-disable-next-line typescript/no-unnecessary-type-assertion
553 (x) => `count: ${(x as { count: number }).count}`,
554 ),
555 }),
556 })
557​
558 expectTypeOf(result).toEqualTypeOf<Promise<{ pages: Array<string> }>>()
559 })
560​
561 it('should allow passing pages', async () => {
562 const result = await new QueryClient().infiniteQuery({
563 queryKey: ['key'],
564 queryFn: () => Promise.resolve({ count: 1 }),
565 getNextPageParam: () => 1,
566 initialPageParam: 1,
567 pages: 5,
568 })
569​
570 expectTypeOf(result).toEqualTypeOf<
571 InfiniteData<{ count: number }, number>
572 >()
573 })
574​
575 it('should type pages as a number', () => {
576 expectTypeOf<
577 Extract<
578 InfiniteQueryExecuteOptions<
579 { count: number },
580 Error,
581 unknown,
582 QueryKey,
583 number
584 >,
585 { pages: unknown }
586 >['pages']
587 >().toEqualTypeOf<number>()
588 })
589​
590 it('should allow passing getNextPageParam without pages', () => {
591 assertType<Parameters<QueryClient['infiniteQuery']>>([
592 {
593 queryKey: ['key'],
594 queryFn: () => Promise.resolve({ count: 1 }),
595 initialPageParam: 1,
596 getNextPageParam: () => 1,
597 },
598 ])
599 })
600​
601 it('should not allow passing pages without getNextPageParam', () => {
602 assertType<Parameters<QueryClient['infiniteQuery']>>([
603 // @ts-expect-error Property 'getNextPageParam' is missing
604 {
605 queryKey: ['key'],
606 queryFn: () => Promise.resolve('string'),
607 initialPageParam: 1,
608 pages: 5,
609 },
610 ])
611 })
612​
613 it('should default its page param to unknown', () => {
614 const queryClient = new QueryClient()
615 const result = queryClient.infiniteQuery({
616 queryKey: ['key'],
617 queryFn: () => Promise.resolve('string'),
618 initialPageParam: 1,
619 getNextPageParam: () => 2,
620 })
621​
622 expectTypeOf(result).toEqualTypeOf<
623 Promise<InfiniteData<string, number>>
624 >()
625​
626 type PageParam = Parameters<
627 typeof queryClient.infiniteQuery<string, Error>
628 >[0]['initialPageParam']
629 expectTypeOf<PageParam>().toEqualTypeOf<unknown>()
630 })
631​
632 it('should resolve with InfiniteData of the data type', async () => {
633 const queryClient = new QueryClient()
634 const data = await queryClient.infiniteQuery(typedInfiniteQueryOptions)
635​
636 expectTypeOf(data).toEqualTypeOf<InfiniteData<TypedData, unknown>>()
637 })
638​
639 it('should resolve with InfiniteData of unknown when untyped', async () => {
640 const queryClient = new QueryClient()
641 const data = await queryClient.infiniteQuery(
642 untypedFetchInfiniteQueryOptions,
643 )
644​
645 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, unknown>>()
646 })
647 })
648​
649 describe('ensureInfiniteQueryData', () => {
650 it('should resolve with InfiniteData of the data type', async () => {
651 const queryClient = new QueryClient()
652 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
653 const data = await queryClient.ensureInfiniteQueryData(
654 typedFetchInfiniteQueryOptions,
655 )
656​
657 expectTypeOf(data).toEqualTypeOf<InfiniteData<TypedData, unknown>>()
658 })
659​
660 it('should resolve with InfiniteData of unknown when untyped', async () => {
661 const queryClient = new QueryClient()
662 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
663 const data = await queryClient.ensureInfiniteQueryData(
664 untypedFetchInfiniteQueryOptions,
665 )
666​
667 expectTypeOf(data).toEqualTypeOf<InfiniteData<unknown, unknown>>()
668 })
669​
670 it('should accept a revalidateIfStale that FetchInfiniteQueryOptions does not have', () => {
671 const queryClient = new QueryClient()
672​
673 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
674 queryClient.ensureInfiniteQueryData({
675 ...typedFetchInfiniteQueryOptions,
676 revalidateIfStale: true,
677 })
678​
679 type RevalidateIfStale = NonNullable<
680 Parameters<QueryClient['ensureInfiniteQueryData']>[0]
681 >['revalidateIfStale']
682 expectTypeOf<RevalidateIfStale>().toEqualTypeOf<boolean | undefined>()
683 })
684 })
685​
686 describe('invalidateQueries', () => {
687 it('should reject a non-array queryKey', () => {
688 assertType<Parameters<QueryClient['invalidateQueries']>>([])
689 assertType<Parameters<QueryClient['invalidateQueries']>>([
690 { queryKey: ['1'] },
691 ])
692 assertType<Parameters<QueryClient['invalidateQueries']>>([
693 // @ts-expect-error
694 { queryKey: '1' },
695 ])
696 })
697 it('should reject an object queryKey (#8684)', () => {
698 assertType<Parameters<QueryClient['invalidateQueries']>>([
699 // @ts-expect-error key is not an array
700 { queryKey: { foo: true } },
701 ])
702 })
703 it('should type the predicate if the key is tagged', () => {
704 const key = ['key'] as DataTag<Array<string>, number>
705 const queryClient = new QueryClient()
706 queryClient.invalidateQueries({
707 queryKey: key,
708 predicate: (query) => {
709 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
710 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
711 return true
712 },
713 })
714 })
715​
716 it('should return a promise of void', () => {
717 const queryClient = new QueryClient()
718​
719 expectTypeOf(queryClient.invalidateQueries()).toEqualTypeOf<
720 Promise<void>
721 >()
722 })
723​
724 it('should accept a refetchType that QueryFilters does not have', () => {
725 type RefetchType = NonNullable<
726 Parameters<QueryClient['invalidateQueries']>[0]
727 >['refetchType']
728 expectTypeOf<RefetchType>().toEqualTypeOf<
729 'all' | 'active' | 'inactive' | 'none' | undefined
730 >()
731 })
732​
733 it('should accept the refetch options it inherits as its second argument', () => {
734 expectTypeOf<
735 NonNullable<Parameters<QueryClient['invalidateQueries']>[1]>
736 >().toEqualTypeOf<{
737 throwOnError?: boolean
738 cancelRefetch?: boolean
739 }>()
740 })
741 })
742​
743 describe('cancelQueries', () => {
744 it('should type the predicate if the key is tagged', () => {
745 const key = ['key'] as DataTag<Array<string>, number>
746 const queryClient = new QueryClient()
747 queryClient.cancelQueries({
748 queryKey: key,
749 predicate: (query) => {
750 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
751 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
752 return true
753 },
754 })
755 })
756​
757 it('should accept being called without filters', () => {
758 assertType<Parameters<QueryClient['cancelQueries']>>([])
759 })
760​
761 it('should return a promise of void', () => {
762 const queryClient = new QueryClient()
763​
764 expectTypeOf(queryClient.cancelQueries()).toEqualTypeOf<Promise<void>>()
765 })
766 })
767​
768 describe('removeQueries', () => {
769 it('should type the predicate if the key is tagged', () => {
770 const key = ['key'] as DataTag<Array<string>, number>
771 const queryClient = new QueryClient()
772 queryClient.removeQueries({
773 queryKey: key,
774 predicate: (query) => {
775 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
776 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
777 return true
778 },
779 })
780 })
781​
782 it('should accept being called without filters', () => {
783 assertType<Parameters<QueryClient['removeQueries']>>([])
784 })
785​
786 it('should return void', () => {
787 const queryClient = new QueryClient()
788​
789 expectTypeOf(queryClient.removeQueries()).toEqualTypeOf<void>()
790 })
791 })
792​
793 describe('refetchQueries', () => {
794 it('should type the predicate if the key is tagged', () => {
795 const key = ['key'] as DataTag<Array<string>, number>
796 const queryClient = new QueryClient()
797 queryClient.refetchQueries({
798 queryKey: key,
799 predicate: (query) => {
800 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
801 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
802 return true
803 },
804 })
805 })
806​
807 it('should accept being called without filters', () => {
808 assertType<Parameters<QueryClient['refetchQueries']>>([])
809 })
810​
811 it('should return a promise of void', () => {
812 const queryClient = new QueryClient()
813​
814 expectTypeOf(queryClient.refetchQueries()).toEqualTypeOf<Promise<void>>()
815 })
816 })
817​
818 describe('resetQueries', () => {
819 it('should type the predicate if the key is tagged', () => {
820 const key = ['key'] as DataTag<Array<string>, number>
821 const queryClient = new QueryClient()
822 queryClient.resetQueries({
823 queryKey: key,
824 predicate: (query) => {
825 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
826 expectTypeOf(query.queryKey).toEqualTypeOf<QueryKey>()
827 return true
828 },
829 })
830 })
831​
832 it('should accept being called without filters', () => {
833 assertType<Parameters<QueryClient['resetQueries']>>([])
834 })
835​
836 it('should return a promise of void', () => {
837 const queryClient = new QueryClient()
838​
839 expectTypeOf(queryClient.resetQueries()).toEqualTypeOf<Promise<void>>()
840 })
841​
842 it('should accept the refetch options it inherits as its second argument', () => {
843 expectTypeOf<
844 NonNullable<Parameters<QueryClient['resetQueries']>[1]>
845 >().toEqualTypeOf<{
846 throwOnError?: boolean
847 cancelRefetch?: boolean
848 }>()
849 })
850 })
851​
852 describe('isFetching', () => {
853 it('should return a number', () => {
854 const queryClient = new QueryClient()
855​
856 expectTypeOf(queryClient.isFetching()).toEqualTypeOf<number>()
857 expectTypeOf(
858 queryClient.isFetching({ queryKey: ['key'] }),
859 ).toEqualTypeOf<number>()
860 })
861​
862 it('should accept being called without filters', () => {
863 assertType<Parameters<QueryClient['isFetching']>>([])
864 })
865​
866 it('should reject an unknown filter property', () => {
867 assertType<Parameters<QueryClient['isFetching']>>([
868 // @ts-expect-error unknown filter properties are rejected
869 { notAFilter: true },
870 ])
871 })
872 })
873​
874 describe('isMutating', () => {
875 it('should return a number', () => {
876 const queryClient = new QueryClient()
877​
878 expectTypeOf(queryClient.isMutating()).toEqualTypeOf<number>()
879 expectTypeOf(
880 queryClient.isMutating({ mutationKey: ['key'] }),
881 ).toEqualTypeOf<number>()
882 })
883​
884 it('should accept being called without filters', () => {
885 assertType<Parameters<QueryClient['isMutating']>>([])
886 })
887​
888 it('should reject an unknown filter property', () => {
889 assertType<Parameters<QueryClient['isMutating']>>([
890 // @ts-expect-error unknown filter properties are rejected
891 { notAFilter: true },
892 ])
893 })
894 it('should accept typed and untyped mutation filters alike', () => {
895 const queryClient = new QueryClient()
896​
897 expectTypeOf(
898 queryClient.isMutating(typedMutationFilters),
899 ).toEqualTypeOf<number>()
900 expectTypeOf(
901 queryClient.isMutating(untypedMutationFilters),
902 ).toEqualTypeOf<number>()
903 })
904 })
905​
906 describe('getQueriesData', () => {
907 it('should require filters', () => {
908 const queryClient = new QueryClient()
909​
910 expectTypeOf(
911 queryClient.getQueriesData({ queryKey: ['key'] }),
912 ).toEqualTypeOf<Array<[QueryKey, unknown]>>()
913​
914 // @ts-expect-error filters are required
915 queryClient.getQueriesData()
916 })
917​
918 it('should infer the passed generic as the tuple data', () => {
919 const queryClient = new QueryClient()
920 const data = queryClient.getQueriesData<number>({ queryKey: ['key'] })
921​
922 expectTypeOf(data).toEqualTypeOf<Array<[QueryKey, number | undefined]>>()
923 })
924​
925 it('should widen the tuple to the filter key and unknown when tagged', () => {
926 const queryClient = new QueryClient()
927 const data = queryClient.getQueriesData(typedQueryFilters)
928​
929 expectTypeOf(data).toEqualTypeOf<
930 Array<[ReadonlyArray<unknown>, unknown]>
931 >()
932 })
933​
934 it('should resolve QueryKey and unknown when untyped', () => {
935 const queryClient = new QueryClient()
936 const data = queryClient.getQueriesData(untypedQueryFilters)
937​
938 expectTypeOf(data).toEqualTypeOf<Array<[QueryKey, unknown]>>()
939 })
940 })
941​
942 describe('prefetchQuery', () => {
943 it('should return a promise of void rather than the data', () => {
944 const queryClient = new QueryClient()
945 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
946 const result = queryClient.prefetchQuery({
947 queryKey: ['key'],
948 queryFn: () => Promise.resolve('string'),
949 })
950​
951 expectTypeOf(result).toEqualTypeOf<Promise<void>>()
952 })
953 })
954​
955 describe('prefetchInfiniteQuery', () => {
956 it('should return a promise of void rather than the pages', () => {
957 const queryClient = new QueryClient()
958 // oxlint-disable-next-line tanstack-query/no-restricted-syntax -- grandfathered direct test
959 const result = queryClient.prefetchInfiniteQuery({
960 queryKey: ['key'],
961 queryFn: () => Promise.resolve('string'),
962 initialPageParam: 1,
963 getNextPageParam: () => 2,
964 })
965​
966 expectTypeOf(result).toEqualTypeOf<Promise<void>>()
967 })
968 })
969​
970 describe('resumePausedMutations', () => {
971 it('should return a promise of unknown', () => {
972 const queryClient = new QueryClient()
973​
974 expectTypeOf(queryClient.resumePausedMutations()).toEqualTypeOf<
975 Promise<unknown>
976 >()
977 })
978 })
979​
980 describe('mount', () => {
981 it('should take no arguments and return void', () => {
982 const queryClient = new QueryClient()
983​
984 expectTypeOf(queryClient.mount).parameters.toEqualTypeOf<[]>()
985 expectTypeOf(queryClient.mount()).toEqualTypeOf<void>()
986 })
987 })
988​
989 describe('unmount', () => {
990 it('should take no arguments and return void', () => {
991 const queryClient = new QueryClient()
992​
993 expectTypeOf(queryClient.unmount).parameters.toEqualTypeOf<[]>()
994 expectTypeOf(queryClient.unmount()).toEqualTypeOf<void>()
995 })
996 })
997​
998 describe('clear', () => {
999 it('should take no arguments and return void', () => {
1000 const queryClient = new QueryClient()
1001​
1002 expectTypeOf(queryClient.clear).parameters.toEqualTypeOf<[]>()
1003 expectTypeOf(queryClient.clear()).toEqualTypeOf<void>()
1004 })
1005 })
1006​
1007 describe('getQueryCache', () => {
1008 it('should return the QueryCache', () => {
1009 const queryClient = new QueryClient()
1010​
1011 expectTypeOf(queryClient.getQueryCache()).toEqualTypeOf<QueryCache>()
1012 })
1013 })
1014​
1015 describe('getMutationCache', () => {
1016 it('should return the MutationCache', () => {
1017 const queryClient = new QueryClient()
1018​
1019 expectTypeOf(
1020 queryClient.getMutationCache(),
1021 ).toEqualTypeOf<MutationCache>()
1022 })
1023 })
1024​
1025 describe('getDefaultOptions', () => {
1026 it('should return the DefaultOptions', () => {
1027 const queryClient = new QueryClient()
1028​
1029 expectTypeOf(
1030 queryClient.getDefaultOptions(),
1031 ).toEqualTypeOf<DefaultOptions>()
1032 })
1033 })
1034​
1035 describe('setDefaultOptions', () => {
1036 it('should only accept DefaultOptions', () => {
1037 expectTypeOf<
1038 Parameters<QueryClient['setDefaultOptions']>[0]
1039 >().toEqualTypeOf<DefaultOptions>()
1040​
1041 assertType<Parameters<QueryClient['setDefaultOptions']>>([
1042 // @ts-expect-error unknown top-level options are rejected
1043 { notAnOption: true },
1044 ])
1045 })
1046 })
1047​
1048 describe('setQueryDefaults', () => {
1049 it('should only accept a QueryKey as its first argument', () => {
1050 const queryClient = new QueryClient()
1051​
1052 queryClient.setQueryDefaults(['key'], { staleTime: 1000 })
1053​
1054 expectTypeOf<
1055 Parameters<QueryClient['setQueryDefaults']>[0]
1056 >().toEqualTypeOf<QueryKey>()
1057​
1058 assertType<Parameters<QueryClient['setQueryDefaults']>>([
1059 // @ts-expect-error the query key must be an array
1060 'key',
1061 {},
1062 ])
1063 })
1064​
1065 it('should make every option optional and reject queryKey', () => {
1066 const queryClient = new QueryClient()
1067​
1068 queryClient.setQueryDefaults(['key'], {})
1069 queryClient.setQueryDefaults(['key'], { staleTime: 1000 })
1070​
1071 expectTypeOf<
1072 Parameters<typeof queryClient.setQueryDefaults>[1]
1073 >().not.toHaveProperty('queryKey')
1074​
1075 queryClient.setQueryDefaults(['key'], {
1076 // @ts-expect-error queryKey is omitted from the defaults
1077 queryKey: ['key'],
1078 })
1079 })
1080​
1081 it('should return void', () => {
1082 const queryClient = new QueryClient()
1083​
1084 expectTypeOf(
1085 queryClient.setQueryDefaults(['key'], {}),
1086 ).toEqualTypeOf<void>()
1087 })
1088 })
1089​
1090 describe('getQueryDefaults', () => {
1091 it('should require a QueryKey', () => {
1092 const queryClient = new QueryClient()
1093​
1094 expectTypeOf<
1095 Parameters<QueryClient['getQueryDefaults']>[0]
1096 >().toEqualTypeOf<QueryKey>()
1097​
1098 // @ts-expect-error the query key is required
1099 queryClient.getQueryDefaults()
1100 })
1101​
1102 it('should return the query options without queryKey', () => {
1103 const queryClient = new QueryClient()
1104 const defaults = queryClient.getQueryDefaults(['key'])
1105​
1106 expectTypeOf(defaults).toEqualTypeOf<
1107 OmitKeyof<QueryObserverOptions<any, any, any, any, any>, 'queryKey'>
1108 >()
1109 expectTypeOf(defaults).not.toHaveProperty('queryKey')
1110 })
1111​
1112 it('should resolve the same options for a tagged and an untyped key', () => {
1113 const queryClient = new QueryClient()
1114​
1115 expectTypeOf(queryClient.getQueryDefaults(typedFilterKey)).toEqualTypeOf<
1116 OmitKeyof<QueryObserverOptions<any, any, any, any, any>, 'queryKey'>
1117 >()
1118 expectTypeOf(
1119 queryClient.getQueryDefaults(untypedFilterKey),
1120 ).toEqualTypeOf<
1121 OmitKeyof<QueryObserverOptions<any, any, any, any, any>, 'queryKey'>
1122 >()
1123 })
1124 })
1125​
1126 describe('setMutationDefaults', () => {
1127 it('should only accept a MutationKey as its first argument', () => {
1128 const queryClient = new QueryClient()
1129​
1130 queryClient.setMutationDefaults(['key'], {})
1131​
1132 expectTypeOf<
1133 Parameters<QueryClient['setMutationDefaults']>[0]
1134 >().toEqualTypeOf<MutationKey>()
1135​
1136 assertType<Parameters<QueryClient['setMutationDefaults']>>([
1137 // @ts-expect-error the mutation key must be an array
1138 'key',
1139 {},
1140 ])
1141 })
1142​
1143 it('should reject mutationKey inside the defaults', () => {
1144 const queryClient = new QueryClient()
1145​
1146 queryClient.setMutationDefaults(['key'], { retry: 3 })
1147​
1148 expectTypeOf<
1149 Parameters<typeof queryClient.setMutationDefaults>[1]
1150 >().not.toHaveProperty('mutationKey')
1151​
1152 queryClient.setMutationDefaults(['key'], {
1153 // @ts-expect-error mutationKey is omitted from the defaults
1154 mutationKey: ['key'],
1155 })
1156 })
1157​
1158 it('should return void', () => {
1159 const queryClient = new QueryClient()
1160​
1161 expectTypeOf(
1162 queryClient.setMutationDefaults(['key'], {}),
1163 ).toEqualTypeOf<void>()
1164 })
1165​
1166 // oxlint-disable-next-line vitest/expect-expect -- Oxlint does not find assertions inside object methods
1167 it('should type the onSettled callback arguments', () => {
1168 const queryClient = new QueryClient()
1169​
1170 queryClient.setMutationDefaults(typedMutationKey, {
1171 onSettled(data, error, variables, onMutateResult, context) {
1172 expectTypeOf(data).toEqualTypeOf<unknown>()
1173 expectTypeOf(error).toEqualTypeOf<DefaultError | null>()
1174 expectTypeOf(variables).toEqualTypeOf<void>()
1175 expectTypeOf(onMutateResult).toEqualTypeOf<unknown>()
1176 expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
1177 },
1178 })
1179 })
1180 })
1181​
1182 describe('getMutationDefaults', () => {
1183 it('should require a MutationKey', () => {
1184 const queryClient = new QueryClient()
1185​
1186 expectTypeOf<
1187 Parameters<QueryClient['getMutationDefaults']>[0]
1188 >().toEqualTypeOf<MutationKey>()
1189​
1190 // @ts-expect-error the mutation key is required
1191 queryClient.getMutationDefaults()
1192 })
1193​
1194 it('should return the mutation options without mutationKey', () => {
1195 const queryClient = new QueryClient()
1196 const defaults = queryClient.getMutationDefaults(['key'])
1197​
1198 expectTypeOf(defaults).toEqualTypeOf<
1199 OmitKeyof<MutationObserverOptions<any, any, any, any>, 'mutationKey'>
1200 >()
1201 expectTypeOf(defaults).not.toHaveProperty('mutationKey')
1202 })
1203​
1204 it('should accept a mutation key taken from typed mutation options', () => {
1205 const queryClient = new QueryClient()
1206​
1207 expectTypeOf(
1208 queryClient.getMutationDefaults(typedMutationKey),
1209 ).toEqualTypeOf<
1210 OmitKeyof<MutationObserverOptions<any, any, any, any>, 'mutationKey'>
1211 >()
1212 expectTypeOf(
1213 queryClient.getMutationDefaults(untypedMutationKey),
1214 ).toEqualTypeOf<
1215 OmitKeyof<MutationObserverOptions<any, any, any, any>, 'mutationKey'>
1216 >()
1217 })
1218 })
1219​
1220 describe('defaultQueryOptions', () => {
1221 it('should resolve the defaulted options from the data and error types', () => {
1222 const queryClient = new QueryClient()
1223 const options = queryClient.defaultQueryOptions(typedQueryOptions)
1224​
1225 expectTypeOf(options).toEqualTypeOf<
1226 DefaultedQueryObserverOptions<
1227 TypedData,
1228 TypedError,
1229 TypedData,
1230 TypedData,
1231 QueryKey
1233 >()
1234 })
1235​
1236 it('should resolve unknown and DefaultError when untyped', () => {
1237 const queryClient = new QueryClient()
1238 const options = queryClient.defaultQueryOptions(untypedQueryOptions)
1239​
1240 expectTypeOf(options).toEqualTypeOf<
1241 DefaultedQueryObserverOptions<
1242 unknown,
1243 DefaultError,
1244 unknown,
1245 unknown,
1246 QueryKey
1248 >()
1249 })
1250 })
1251​
1252 describe('defaultMutationOptions', () => {
1253 it('should accept being called without options', () => {
1254 assertType<Parameters<QueryClient['defaultMutationOptions']>>([])
1255 })
1256​
1257 it('should return the same type it is given', () => {
1258 const queryClient = new QueryClient()
1259 const options: MutationOptions<number, Error, string> = {}
1260 const defaulted = queryClient.defaultMutationOptions(options)
1261​
1262 expectTypeOf(defaulted).toEqualTypeOf<
1263 MutationOptions<number, Error, string>
1264 >()
1265 })
1266​
1267 it('should default its variables to void and its context to unknown', () => {
1268 const queryClient = new QueryClient()
1269​
1270 expectTypeOf(
1271 queryClient.defaultMutationOptions(typedMutationOptions),
1272 ).toEqualTypeOf<MutationOptions<TypedData, TypedError, void, unknown>>()
1273 expectTypeOf(
1274 queryClient.defaultMutationOptions(untypedMutationOptions),
1275 ).toEqualTypeOf<MutationOptions<unknown, DefaultError, void, unknown>>()
1276 })
1277 })
1278​
1279 describe('QueryClientConfig', () => {
1280 it('should be optional', () => {
1281 assertType<ConstructorParameters<typeof QueryClient>>([])
1282 })
1283​
1284 it('should declare exactly its three options', () => {
1285 expectTypeOf<keyof QueryClientConfig>().toEqualTypeOf<
1286 'queryCache' | 'mutationCache' | 'defaultOptions'
1287 >()
1288 })
1289​
1290 it('should declare every option as optional', () => {
1291 type OptionalKeys = {
1292 [K in keyof QueryClientConfig]-?: {} extends Pick<QueryClientConfig, K>
1293 ? K
1294 : never
1295 }[keyof QueryClientConfig]
1296​
1297 expectTypeOf<OptionalKeys>().toEqualTypeOf<keyof QueryClientConfig>()
1298 })
1299​
1300 it('should reject unknown options', () => {
1301 assertType<ConstructorParameters<typeof QueryClient>>([
1302 // @ts-expect-error unknown config options are rejected
1303 { notAnOption: true },
1304 ])
1305 })
1306​
1307 describe('defaultOptions', () => {
1308 it('should have a typed QueryFunctionContext', () => {
1309 new QueryClient({
1310 defaultOptions: {
1311 queries: {
1312 queryFn: (context) => {
1313 expectTypeOf(context).toEqualTypeOf<{
1314 client: QueryClient
1315 queryKey: QueryKey
1316 meta: Record<string, unknown> | undefined
1317 signal: AbortSignal
1318 pageParam?: unknown
1319 direction?: unknown
1320 }>()
1321 return Promise.resolve('data')
1322 },
1323 },
1324 },
1325 })
1326 })
1327 })
1328 })
1329​
1330 describe('DataTag', () => {
1331 it('should default its error to UnsetMarker so an untagged error falls back', () => {
1332 const key = ['key'] as DataTag<Array<string>, number>
1333 const queryClient = new QueryClient()
1334​
1335 expectTypeOf(queryClient.getQueryState(key)).toEqualTypeOf<
1336 QueryState<number, Error> | undefined
1337 >()
1338​
1339 type Tagged = DataTag<Array<string>, number>
1340 type TaggedError =
1341 Tagged extends DataTag<unknown, unknown, infer E> ? E : never
1342 expectTypeOf<TaggedError>().toEqualTypeOf<UnsetMarker>()
1343 })
1344​
1345 it('should not re-tag a key that is already tagged', () => {
1346 type Tagged = DataTag<Array<string>, number>
1347​
1348 // re-tagging returns the original tag rather than layering a second one
1349 expectTypeOf<DataTag<Tagged, string>>().toEqualTypeOf<Tagged>()
1350​
1351 const queryClient = new QueryClient()
1352 const reTagged = ['key'] as DataTag<Tagged, string>
1353​
1354 expectTypeOf(queryClient.getQueryData(reTagged)).toEqualTypeOf<
1355 number | undefined
1356 >()
1357 })
1358 })
1359​
1360 describe('QueryFilters', () => {
1361 it('should type every filter property', () => {
1362 expectTypeOf<QueryFilters['type']>().toEqualTypeOf<
1363 'all' | 'active' | 'inactive' | undefined
1364 >()
1365 expectTypeOf<QueryFilters['exact']>().toEqualTypeOf<boolean | undefined>()
1366 expectTypeOf<QueryFilters['stale']>().toEqualTypeOf<boolean | undefined>()
1367 expectTypeOf<QueryFilters['fetchStatus']>().toEqualTypeOf<
1368 'fetching' | 'paused' | 'idle' | undefined
1369 >()
1370 })
1371​
1372 it('should keep every filter property writable', () => {
1373 expectTypeOf<QueryFilters>().toEqualTypeOf<{
1374 -readonly [K in keyof QueryFilters]: QueryFilters[K]
1375 }>()
1376 })
1377​
1378 it('should reject a non-QueryTypeFilter type', () => {
1379 assertType<Parameters<QueryClient['isFetching']>>([
1380 // @ts-expect-error 'paused' is not a QueryTypeFilter
1381 { type: 'paused' },
1382 ])
1383 })
1384​
1385 it('should type the query given to a predicate as fully unknown', () => {
1386 const filters: QueryFilters<DataTag<QueryKey, TypedData, TypedError>> = {
1387 predicate(query) {
1388 expectTypeOf(query).toEqualTypeOf<
1389 Query<unknown, Error, unknown, ReadonlyArray<unknown>>
1390 >()
1391 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
1392 expectTypeOf(query.state.error).toEqualTypeOf<Error | null>()
1393 return false
1394 },
1396 expectTypeOf(filters.predicate).toEqualTypeOf<
1397 ((query: Query) => boolean) | undefined
1398 >()
1399 })
1400​
1401 it('should type the query given to an untyped predicate', () => {
1402 const filters: QueryFilters = {
1403 predicate(query) {
1404 expectTypeOf(query).toEqualTypeOf<Query<unknown, DefaultError>>()
1405 expectTypeOf(query.state.data).toEqualTypeOf<unknown>()
1406 expectTypeOf(query.state.error).toEqualTypeOf<DefaultError | null>()
1407 return false
1408 },
1410 expectTypeOf(filters.predicate).toEqualTypeOf<
1411 ((query: Query) => boolean) | undefined
1412 >()
1413 })
1414 })
1415​
1416 describe('MutationFilters', () => {
1417 it('should reject a mutationKey that is not an array', () => {
1418 assertType<Parameters<QueryClient['isMutating']>>([
1419 // @ts-expect-error the mutation key must be an array
1420 { mutationKey: 'key' },
1421 ])
1422 })
1423​
1424 it('should type the mutation given to a predicate from its generics', () => {
1425 const filters: MutationFilters<TypedData, TypedError> = {
1426 predicate(mutation) {
1427 expectTypeOf(mutation).toEqualTypeOf<
1428 Mutation<TypedData, TypedError>
1429 >()
1430 expectTypeOf(mutation.state.data).toEqualTypeOf<
1431 TypedData | undefined
1432 >()
1433 expectTypeOf(mutation.state.error).toEqualTypeOf<TypedError | null>()
1434 return false
1435 },
1437 expectTypeOf(filters.predicate).toEqualTypeOf<
1438 ((mutation: Mutation<TypedData, TypedError>) => boolean) | undefined
1439 >()
1440 })
1441​
1442 it('should type the mutation given to an untyped predicate', () => {
1443 const filters: MutationFilters = {
1444 predicate(mutation) {
1445 expectTypeOf(mutation).toEqualTypeOf<Mutation>()
1446 expectTypeOf(mutation.state.data).toEqualTypeOf<unknown>()
1447 expectTypeOf(
1448 mutation.state.error,
1449 ).toEqualTypeOf<DefaultError | null>()
1450 return false
1451 },
1453 expectTypeOf(filters.predicate).toEqualTypeOf<
1454 ((mutation: Mutation) => boolean) | undefined
1455 >()
1456 })
1457 })
1458​
1459 describe('Updater', () => {
1460 it('should require the previous data argument on the function branch', () => {
1461 const key = ['key'] as DataTag<Array<string>, number>
1462 const queryClient = new QueryClient()
1463​
1464 type UpdaterArg = Parameters<
1465 typeof queryClient.setQueryData<number, typeof key>
1466 >[1]
1467 expectTypeOf<UpdaterArg>().toEqualTypeOf<
1468 Updater<number | undefined, number | undefined>
1469 >()
1470 expectTypeOf<Extract<UpdaterArg, (...args: any) => any>>().toEqualTypeOf<
1471 (input: number | undefined) => number | undefined
1472 >()
1473 })
1474 })