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

useQueries.test-d.tsx

872 lines27.0 KB

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