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