Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { skipToken } from '@tanstack/query-core'
3import { queryKey } from '@tanstack/query-test-utils'
4import { queryOptions, useQueries } from '..'
5import { QueryClient } from '../QueryClient'
6import type * as QueryCore from '@tanstack/query-core'
7import type { OmitKeyof } from '@tanstack/query-core'
8import type {
9 QueryFunction,
10 QueryFunctionContext,
11 QueryKey,
12 UseQueryResult,
13} from '..'
14import type { QueryOptions } from '../types'
15β
16describe('useQueries', () => {
17 it('should have undefined in the TData union even when initialData is provided as an object', () => {
18 const query1 = {
19 queryKey: queryKey(),
20 queryFn: () => {
21 return {
22 wow: true,
23 }
24 },
25 initialData: {
26 wow: false,
27 },
28 }
29β
30 const query2 = {
31 queryKey: queryKey(),
32 queryFn: () => 'Query Data',
33 initialData: 'initial data',
34 }
35β
36 const query3 = {
37 queryKey: queryKey(),
38 queryFn: () => 'Query Data',
39 }
40β
41 const queryResults = useQueries(() => ({
42 queries: [query1, query2, query3],
43 }))
44β
45 const query1Data = queryResults[0].data
46 const query2Data = queryResults[1].data
47 const query3Data = queryResults[2].data
48β
49 expectTypeOf(query1Data).toEqualTypeOf<{ wow: boolean } | undefined>()
50 expectTypeOf(query2Data).toEqualTypeOf<string | undefined>()
51 expectTypeOf(query3Data).toEqualTypeOf<string | undefined>()
52 })
53β
54 it('should have undefined in the TData union when passed through queryOptions', () => {
55 const options = queryOptions({
56 queryKey: queryKey(),
57 queryFn: () => {
58 return {
59 wow: true,
60 }
61 },
62 initialData: {
63 wow: true,
64 },
65 })
66 const queryResults = useQueries(() => ({ queries: [options] }))
67β
68 const data = queryResults[0].data
69β
70 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
71 })
72β
73 it('should have undefined in the TData union when initialData is provided as a function which can return undefined', () => {
74 const queryResults = useQueries(() => ({
75 queries: [
76 {
77 queryKey: queryKey(),
78 queryFn: () => {
79 return {
80 wow: true,
81 }
82 },
83 initialData: () => undefined as { wow: boolean } | undefined,
84 },
85 ],
86 }))
87β
88 const data = queryResults[0].data
89β
90 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
91 })
92β
93 it('should infer types from explicit object type parameter', () => {
94 const queryResults = useQueries<
95 [
96 { queryFnData: number },
97 { queryFnData: string; error: Error },
98 { queryFnData: boolean; data: string },
99 ]
100 >(() => ({
101 queries: [
102 {
103 queryKey: queryKey(),
104 queryFn: () => Promise.resolve(1),
105 },
106 {
107 queryKey: queryKey(),
108 queryFn: () => Promise.resolve('data'),
109 },
110 {
111 queryKey: queryKey(),
112 queryFn: () => Promise.resolve(true),
113 select: () => 'selected',
114 },
115 ],
116 }))
117β
118 expectTypeOf(queryResults[0].data).toEqualTypeOf<number | undefined>()
119 expectTypeOf(queryResults[1].data).toEqualTypeOf<string | undefined>()
120 expectTypeOf(queryResults[1].error).toEqualTypeOf<Error | null>()
121 expectTypeOf(queryResults[2].data).toEqualTypeOf<string | undefined>()
122 })
123β
124 it('should infer types from explicit tuple type parameter', () => {
125 const queryResults = useQueries<
126 [[number], [string, Error], [boolean, Error, string]]
127 >(() => ({
128 queries: [
129 {
130 queryKey: queryKey(),
131 queryFn: () => Promise.resolve(1),
132 },
133 {
134 queryKey: queryKey(),
135 queryFn: () => Promise.resolve('data'),
136 },
137 {
138 queryKey: queryKey(),
139 queryFn: () => Promise.resolve(true),
140 select: () => 'selected',
141 },
142 ],
143 }))
144β
145 expectTypeOf(queryResults[0].data).toEqualTypeOf<number | undefined>()
146 expectTypeOf(queryResults[1].data).toEqualTypeOf<string | undefined>()
147 expectTypeOf(queryResults[1].error).toEqualTypeOf<Error | null>()
148 expectTypeOf(queryResults[2].data).toEqualTypeOf<string | undefined>()
149 })
150β
151 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQueries', () => {
152 const query1 = queryOptions({
153 queryKey: queryKey(),
154 queryFn: () => Promise.resolve(1),
155 select: (data) => data > 1,
156 })
157β
158 const query2 = {
159 queryKey: queryKey(),
160 queryFn: () => Promise.resolve(1),
161 select: (data: number) => data > 1,
162 }
163β
164 const queryResults = useQueries(() => ({ queries: [query1, query2] }))
165 const query1Data = queryResults[0].data
166 const query2Data = queryResults[1].data
167β
168 expectTypeOf(query1Data).toEqualTypeOf<boolean | undefined>()
169 expectTypeOf(query2Data).toEqualTypeOf<boolean | undefined>()
170 })
171β
172 describe('custom hook', () => {
173 it('should allow custom hooks using QueryOptions', () => {
174 type Data = string
175β
176 const useCustomQueries = (
177 options?: OmitKeyof<QueryOptions<Data>, 'queryKey' | 'queryFn'>,
178 ) => {
179 return useQueries(() => ({
180 queries: [
181 {
182 ...options,
183 queryKey: queryKey(),
184 queryFn: () => Promise.resolve('data'),
185 },
186 ],
187 }))
188 }
189β
190 const queryResults = useCustomQueries()
191 const data = queryResults[0].data
192β
193 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
194 })
195 })
196β
197 it('should infer custom TError from throwOnError', () => {
198 class CustomError extends Error {
199 code: number
200 constructor(code: number) {
201 super()
202 this.code = code
203 }
204 }
205β
206 const queryResults = useQueries(() => ({
207 queries: [
208 {
209 queryKey: queryKey(),
210 queryFn: () => Promise.resolve('data'),
211 throwOnError: (_error: CustomError) => false,
212 },
213 ],
214 }))
215β
216 expectTypeOf(queryResults[0].error).toEqualTypeOf<CustomError | null>()
217 })
218β
219 it('should have correct TData type when conditional skipToken is passed', () => {
220 const queryResults = useQueries(() => ({
221 queries: [
222 {
223 queryKey: queryKey(),
224 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
225 },
226 ],
227 }))
228β
229 const firstResult = queryResults[0]
230β
231 expectTypeOf(firstResult).toEqualTypeOf<UseQueryResult<number, Error>>()
232 expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>()
233 })
234β
235 it('should return correct data for dynamic queries with mixed result types', () => {
236 const Queries1 = {
237 get: () =>
238 queryOptions({
239 queryKey: queryKey(),
240 queryFn: () => Promise.resolve(1),
241 }),
242 }
243 const Queries2 = {
244 get: () =>
245 queryOptions({
246 queryKey: queryKey(),
247 queryFn: () => Promise.resolve(true),
248 }),
249 }
250β
251 const queries1List = [1, 2, 3].map(() => ({ ...Queries1.get() }))
252 const result = useQueries(() => ({
253 queries: [...queries1List, { ...Queries2.get() }],
254 }))
255β
256 expectTypeOf(result).toEqualTypeOf<
257 [...Array<UseQueryResult<number, Error>>, UseQueryResult<boolean, Error>]
258 >()
259 })
260β
261 it('should accept queryClient as second argument', () => {
262 const queryClient = new QueryClient()
263β
264 const queryResults = useQueries(
265 () => ({
266 queries: [
267 {
268 queryKey: queryKey(),
269 queryFn: () => Promise.resolve('data'),
270 },
271 ],
272 }),
273 () => queryClient,
274 )
275β
276 expectTypeOf(queryResults[0].data).toEqualTypeOf<string | undefined>()
277 })
278β
279 it('should infer correct types for combine callback parameter', () => {
280 useQueries(() => ({
281 queries: [
282 {
283 queryKey: queryKey(),
284 queryFn: () => Promise.resolve(1),
285 },
286 {
287 queryKey: queryKey(),
288 queryFn: () => Promise.resolve('hello'),
289 },
290 ],
291 combine: (results) => {
292 expectTypeOf(results[0]).toEqualTypeOf<UseQueryResult<number, Error>>()
293 expectTypeOf(results[1]).toEqualTypeOf<UseQueryResult<string, Error>>()
294 return results
295 },
296 }))
297 })
298β
299 describe('type parameters', () => {
300 it('should handle type parameter - tuple of tuples', () => {
301 const key1 = queryKey()
302 const key2 = queryKey()
303 const key3 = queryKey()
304β
305 // @ts-expect-error (Page component is not rendered)
306 function Page() {
307 const result1 = useQueries<
308 [[number], [string], [Array<string>, boolean]]
309 >(() => ({
310 queries: [
311 {
312 queryKey: key1,
313 queryFn: () => 1,
314 },
315 {
316 queryKey: key2,
317 queryFn: () => 'string',
318 },
319 {
320 queryKey: key3,
321 queryFn: () => ['string[]'],
322 },
323 ],
324 }))
325 expectTypeOf(result1[0]).toEqualTypeOf<UseQueryResult<number>>()
326 expectTypeOf(result1[1]).toEqualTypeOf<UseQueryResult<string>>()
327 expectTypeOf(result1[2]).toEqualTypeOf<
328 UseQueryResult<Array<string>, boolean>
329 >()
330 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
331 expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
332 expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
333 expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
334β
335 // TData (3rd element) takes precedence over TQueryFnData (1st element)
336 const result2 = useQueries<
337 [[string, unknown, string], [string, unknown, number]]
338 >(() => ({
339 queries: [
340 {
341 queryKey: key1,
342 queryFn: () => 'string',
343 select: (a) => {
344 expectTypeOf(a).toEqualTypeOf<string>()
345 return a.toLowerCase()
346 },
347 },
348 {
349 queryKey: key2,
350 queryFn: () => 'string',
351 select: (a) => {
352 expectTypeOf(a).toEqualTypeOf<string>()
353 return parseInt(a)
354 },
355 },
356 ],
357 }))
358 expectTypeOf(result2[0]).toEqualTypeOf<
359 UseQueryResult<string, unknown>
360 >()
361 expectTypeOf(result2[1]).toEqualTypeOf<
362 UseQueryResult<number, unknown>
363 >()
364 expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
365 expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
366β
367 // types should be enforced
368 useQueries<[[string, unknown, string], [string, boolean, number]]>(
369 () => ({
370 queries: [
371 {
372 queryKey: key1,
373 queryFn: () => 'string',
374 select: (a) => {
375 expectTypeOf(a).toEqualTypeOf<string>()
376 return a.toLowerCase()
377 },
378 placeholderData: 'string',
379 // @ts-expect-error (initialData: string)
380 initialData: 123,
381 },
382 {
383 queryKey: key2,
384 queryFn: () => 'string',
385 select: (a) => {
386 expectTypeOf(a).toEqualTypeOf<string>()
387 return parseInt(a)
388 },
389 placeholderData: 'string',
390 // @ts-expect-error (initialData: string)
391 initialData: 123,
392 },
393 ],
394 }),
395 )
396β
397 // field names should be enforced
398 useQueries<[[string]]>(() => ({
399 queries: [
400 {
401 queryKey: key1,
402 queryFn: () => 'string',
403 },
404 ],
405 }))
406 }
407 })
408β
409 it('should handle type parameter - tuple of objects', () => {
410 const key1 = queryKey()
411 const key2 = queryKey()
412 const key3 = queryKey()
413β
414 // @ts-expect-error (Page component is not rendered)
415 function Page() {
416 const result1 = useQueries<
417 [
418 { queryFnData: number },
419 { queryFnData: string },
420 { queryFnData: Array<string>; error: boolean },
421 ]
422 >(() => ({
423 queries: [
424 {
425 queryKey: key1,
426 queryFn: () => 1,
427 },
428 {
429 queryKey: key2,
430 queryFn: () => 'string',
431 },
432 {
433 queryKey: key3,
434 queryFn: () => ['string[]'],
435 },
436 ],
437 }))
438 expectTypeOf(result1[0]).toEqualTypeOf<
439 UseQueryResult<number, unknown>
440 >()
441 expectTypeOf(result1[1]).toEqualTypeOf<
442 UseQueryResult<string, unknown>
443 >()
444 expectTypeOf(result1[2]).toEqualTypeOf<
445 UseQueryResult<Array<string>, boolean>
446 >()
447 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
448 expectTypeOf(result1[1].data).toEqualTypeOf<string | undefined>()
449 expectTypeOf(result1[2].data).toEqualTypeOf<Array<string> | undefined>()
450 expectTypeOf(result1[2].error).toEqualTypeOf<boolean | null>()
451β
452 // TData (data prop) takes precedence over TQueryFnData (queryFnData prop)
453 const result2 = useQueries<
454 [
455 { queryFnData: string; data: string },
456 { queryFnData: string; data: number },
457 ]
458 >(() => ({
459 queries: [
460 {
461 queryKey: key1,
462 queryFn: () => 'string',
463 select: (a) => {
464 expectTypeOf(a).toEqualTypeOf<string>()
465 return a.toLowerCase()
466 },
467 },
468 {
469 queryKey: key2,
470 queryFn: () => 'string',
471 select: (a) => {
472 expectTypeOf(a).toEqualTypeOf<string>()
473 return parseInt(a)
474 },
475 },
476 ],
477 }))
478 expectTypeOf(result2[0]).toEqualTypeOf<
479 UseQueryResult<string, unknown>
480 >()
481 expectTypeOf(result2[1]).toEqualTypeOf<
482 UseQueryResult<number, unknown>
483 >()
484 expectTypeOf(result2[0].data).toEqualTypeOf<string | undefined>()
485 expectTypeOf(result2[1].data).toEqualTypeOf<number | undefined>()
486β
487 // can pass only TData (data prop) although TQueryFnData will be left unknown
488 const result3 = useQueries<[{ data: string }, { data: number }]>(
489 () => ({
490 queries: [
491 {
492 queryKey: key1,
493 queryFn: () => 'string',
494 select: (a) => {
495 expectTypeOf(a).toEqualTypeOf<unknown>()
496 return a as string
497 },
498 },
499 {
500 queryKey: key2,
501 queryFn: () => 'string',
502 select: (a) => {
503 expectTypeOf(a).toEqualTypeOf<unknown>()
504 return a as number
505 },
506 },
507 ],
508 }),
509 )
510 expectTypeOf(result3[0]).toEqualTypeOf<
511 UseQueryResult<string, unknown>
512 >()
513 expectTypeOf(result3[1]).toEqualTypeOf<
514 UseQueryResult<number, unknown>
515 >()
516 expectTypeOf(result3[0].data).toEqualTypeOf<string | undefined>()
517 expectTypeOf(result3[1].data).toEqualTypeOf<number | undefined>()
518β
519 // types should be enforced
520 useQueries<
521 [
522 { queryFnData: string; data: string },
523 { queryFnData: string; data: number; error: boolean },
524 ]
525 >(() => ({
526 queries: [
527 {
528 queryKey: key1,
529 queryFn: () => 'string',
530 select: (a) => {
531 expectTypeOf(a).toEqualTypeOf<string>()
532 return a.toLowerCase()
533 },
534 placeholderData: 'string',
535 // @ts-expect-error (initialData: string)
536 initialData: 123,
537 },
538 {
539 queryKey: key2,
540 queryFn: () => 'string',
541 select: (a) => {
542 expectTypeOf(a).toEqualTypeOf<string>()
543 return parseInt(a)
544 },
545 placeholderData: 'string',
546 // @ts-expect-error (initialData: string)
547 initialData: 123,
548 },
549 ],
550 }))
551β
552 // field names should be enforced
553 useQueries<[{ queryFnData: string }]>(() => ({
554 queries: [
555 {
556 queryKey: key1,
557 queryFn: () => 'string',
558 },
559 ],
560 }))
561 }
562 })
563β
564 it('should handle array literal without type parameter to infer result type', () => {
565 const key1 = queryKey()
566 const key2 = queryKey()
567 const key3 = queryKey()
568 const key4 = queryKey()
569β
570 // @ts-expect-error (Page component is not rendered)
571 function Page() {
572 // Array.map preserves TQueryFnData
573 const result1 = useQueries(() => ({
574 queries: Array(50).map((_, i) => ({
575 queryKey: ['key', i] as const,
576 queryFn: () => i + 10,
577 })),
578 }))
579 expectTypeOf(result1).toEqualTypeOf<
580 Array<UseQueryResult<number, Error>>
581 >()
582 if (result1[0]) {
583 expectTypeOf(result1[0].data).toEqualTypeOf<number | undefined>()
584 }
585β
586 // Array.map preserves TData
587 const result2 = useQueries(() => ({
588 queries: Array(50).map((_, i) => ({
589 queryKey: ['key', i] as const,
590 queryFn: () => i + 10,
591 select: (data: number) => data.toString(),
592 })),
593 }))
594 expectTypeOf(result2).toEqualTypeOf<
595 Array<UseQueryResult<string, Error>>
596 >()
597β
598 const result3 = useQueries(() => ({
599 queries: [
600 {
601 queryKey: key1,
602 queryFn: () => 1,
603 },
604 {
605 queryKey: key2,
606 queryFn: () => 'string',
607 },
608 {
609 queryKey: key3,
610 queryFn: () => ['string[]'],
611 select: () => 123,
612 },
613 ],
614 }))
615 expectTypeOf(result3[0]).toEqualTypeOf<UseQueryResult<number, Error>>()
616 expectTypeOf(result3[1]).toEqualTypeOf<UseQueryResult<string, Error>>()
617 expectTypeOf(result3[2]).toEqualTypeOf<UseQueryResult<number, Error>>()
618 expectTypeOf(result3[0].data).toEqualTypeOf<number | undefined>()
619 expectTypeOf(result3[1].data).toEqualTypeOf<string | undefined>()
620 // select takes precedence over queryFn
621 expectTypeOf(result3[2].data).toEqualTypeOf<number | undefined>()
622β
623 // initialData/placeholderData are enforced
624 useQueries(() => ({
625 queries: [
626 {
627 queryKey: key1,
628 queryFn: () => 'string',
629 placeholderData: 'string',
630 // @ts-expect-error (initialData: string)
631 initialData: 123,
632 },
633 {
634 queryKey: key2,
635 queryFn: () => 123,
636 // @ts-expect-error (placeholderData: number)
637 placeholderData: 'string',
638 initialData: 123,
639 },
640 ],
641 }))
642β
643 // select params are "indirectly" enforced
644 useQueries(() => ({
645 queries: [
646 // unfortunately TS will not suggest the type for you
647 {
648 queryKey: key1,
649 queryFn: () => 'string',
650 },
651 // however you can add a type to the callback
652 {
653 queryKey: key2,
654 queryFn: () => 'string',
655 },
656 // the type you do pass is enforced
657 {
658 queryKey: key3,
659 queryFn: () => 'string',
660 },
661 {
662 queryKey: key4,
663 queryFn: () => 'string',
664 select: (a: string) => parseInt(a),
665 },
666 ],
667 }))
668β
669 // callbacks are also indirectly enforced with Array.map
670 useQueries(() => ({
671 queries: Array(50).map((_, i) => ({
672 queryKey: ['key', i] as const,
673 queryFn: () => i + 10,
674 select: (data: number) => data.toString(),
675 })),
676 }))
677β
678 useQueries(() => ({
679 queries: Array(50).map((_, i) => ({
680 queryKey: ['key', i] as const,
681 queryFn: () => i + 10,
682 select: (data: number) => data.toString(),
683 })),
684 }))
685β
686 // results inference works when all the handlers are defined
687 const result4 = useQueries(() => ({
688 queries: [
689 {
690 queryKey: key1,
691 queryFn: () => 'string',
692 },
693 {
694 queryKey: key2,
695 queryFn: () => 'string',
696 },
697 {
698 queryKey: key4,
699 queryFn: () => 'string',
700 select: (a: string) => parseInt(a),
701 },
702 ],
703 }))
704 expectTypeOf(result4[0]).toEqualTypeOf<UseQueryResult<string, Error>>()
705 expectTypeOf(result4[1]).toEqualTypeOf<UseQueryResult<string, Error>>()
706 expectTypeOf(result4[2]).toEqualTypeOf<UseQueryResult<number, Error>>()
707β
708 // handles when queryFn returns a Promise
709 const result5 = useQueries(() => ({
710 queries: [
711 {
712 queryKey: key1,
713 queryFn: () => Promise.resolve('string'),
714 },
715 ],
716 }))
717 expectTypeOf(result5[0]).toEqualTypeOf<UseQueryResult<string, Error>>()
718β
719 // Array as const does not throw error
720 const result6 = useQueries(
721 () =>
722 ({
723 queries: [
724 {
725 queryKey: ['key1'],
726 queryFn: () => 'string',
727 },
728 {
729 queryKey: ['key1'],
730 queryFn: () => 123,
731 },
732 ],
733 }) as const,
734 )
735 expectTypeOf(result6[0]).toEqualTypeOf<UseQueryResult<string, Error>>()
736 expectTypeOf(result6[1]).toEqualTypeOf<UseQueryResult<number, Error>>()
737β
738 // field names should be enforced - array literal
739 useQueries(() => ({
740 queries: [
741 {
742 queryKey: key1,
743 queryFn: () => 'string',
744 },
745 ],
746 }))
747β
748 // field names should be enforced - Array.map() result
749 useQueries(() => ({
750 // @ts-expect-error (invalidField)
751 queries: Array(10).map(() => ({
752 someInvalidField: '',
753 })),
754 }))
755β
756 // field names should be enforced - array literal
757 useQueries(() => ({
758 queries: [
759 {
760 queryKey: key1,
761 queryFn: () => 'string',
762 },
763 ],
764 }))
765β
766 // supports queryFn using fetch() to return Promise<any> - Array.map() result
767 useQueries(() => ({
768 queries: Array(50).map((_, i) => ({
769 queryKey: ['key', i] as const,
770 queryFn: () =>
771 fetch('return Promise<any>').then((resp) => resp.json()),
772 })),
773 }))
774β
775 // supports queryFn using fetch() to return Promise<any> - array literal
776 useQueries(() => ({
777 queries: [
778 {
779 queryKey: key1,
780 queryFn: () =>
781 fetch('return Promise<any>').then((resp) => resp.json()),
782 },
783 ],
784 }))
785 }
786 })
787β
788 it('should handle strongly typed queryFn factories and useQueries wrappers', () => {
789 // QueryKey + queryFn factory
790 type QueryKeyA = ['queryA']
791 const getQueryKeyA = (): QueryKeyA => ['queryA']
792 type GetQueryFunctionA = () => QueryFunction<number, QueryKeyA>
793 const getQueryFunctionA: GetQueryFunctionA = () => () => {
794 return 1
795 }
796 type SelectorA = (data: number) => [number, string]
797 const getSelectorA = (): SelectorA => (data) => [data, data.toString()]
798β
799 type QueryKeyB = ['queryB', string]
800 const getQueryKeyB = (id: string): QueryKeyB => ['queryB', id]
801 type GetQueryFunctionB = () => QueryFunction<string, QueryKeyB>
802 const getQueryFunctionB: GetQueryFunctionB = () => () => {
803 return '1'
804 }
805 type SelectorB = (data: string) => [string, number]
806 const getSelectorB = (): SelectorB => (data) => [data, +data]
807β
808 // Wrapper with strongly typed array-parameter
809 function useWrappedQueries<
810 TQueryFnData,
811 TError,
812 TData,
813 TQueryKey extends QueryKey,
814 >(queries: Array<QueryOptions<TQueryFnData, TError, TData, TQueryKey>>) {
815 return useQueries(() => ({
816 queries: queries.map(
817 // no need to type the mapped query
818 (query) => {
819 const { queryFn: fn, queryKey: key } = query
820 expectTypeOf(fn).toEqualTypeOf<
821 | typeof QueryCore.skipToken
822 | QueryCore.QueryFunction<TQueryFnData, TQueryKey, never>
823 | undefined
824 >()
825 return {
826 queryKey: key,
827 queryFn: fn
828 ? (ctx: QueryFunctionContext<TQueryKey>) => {
829 // oxlint-disable-next-line vitest-js/valid-expect
830 expectTypeOf<TQueryKey>(ctx.queryKey)
831 return (
832 fn as QueryFunction<TQueryFnData, TQueryKey>
833 ).call({}, ctx)
834 }
835 : undefined,
836 }
837 },
838 ),
839 }))
840 }
841β
842 // @ts-expect-error (Page component is not rendered)
843 function Page() {
844 const result = useQueries(() => ({
845 queries: [
846 {
847 queryKey: getQueryKeyA(),
848 queryFn: getQueryFunctionA(),
849 },
850 {
851 queryKey: getQueryKeyB('id'),
852 queryFn: getQueryFunctionB(),
853 },
854 ],
855 }))
856 expectTypeOf(result[0]).toEqualTypeOf<UseQueryResult<number, Error>>()
857 expectTypeOf(result[1]).toEqualTypeOf<UseQueryResult<string, Error>>()
858β
859 const withSelector = useQueries(() => ({
860 queries: [
861 {
862 queryKey: getQueryKeyA(),
863 queryFn: getQueryFunctionA(),
864 select: getSelectorA(),
865 },
866 {
867 queryKey: getQueryKeyB('id'),
868 queryFn: getQueryFunctionB(),
869 select: getSelectorB(),
870 },
871 ],
872 }))
873 expectTypeOf(withSelector[0]).toEqualTypeOf<
874 UseQueryResult<[number, string], Error>
875 >()
876 expectTypeOf(withSelector[1]).toEqualTypeOf<
877 UseQueryResult<[string, number], Error>
878 >()
879β
880 const withWrappedQueries = useWrappedQueries(
881 Array(10).map(() => ({
882 queryKey: getQueryKeyA(),
883 queryFn: getQueryFunctionA(),
884 select: getSelectorA(),
885 })),
886 )
887β
888 expectTypeOf(withWrappedQueries).toEqualTypeOf<
889 Array<UseQueryResult<number, Error>>
890 >()
891 }
892 })
893 })
894})