@@ -10,6 +10,7 @@ import type {
1010 EnsureQueryDataOptions ,
1111 FetchInfiniteQueryOptions ,
1212 InfiniteData ,
13+ InfiniteQueryExecuteOptions ,
1314 MutationOptions ,
1415 OmitKeyof ,
1516 QueryKey ,
@@ -157,24 +158,55 @@ describe('getQueryState', () => {
157158 } )
158159} )
159160
161+ describe ( 'fetchQuery' , ( ) => {
162+ it ( 'should not allow passing select option' , ( ) => {
163+ assertType < Parameters < QueryClient [ 'fetchQuery' ] > > ( [
164+ {
165+ queryKey : [ 'key' ] ,
166+ queryFn : ( ) => Promise . resolve ( 'string' ) ,
167+ // @ts -expect-error `select` is not supported on fetchQuery options
168+ select : ( data : string ) => data . length ,
169+ } ,
170+ ] )
171+ } )
172+ } )
173+
160174describe ( 'fetchInfiniteQuery' , ( ) => {
175+ it ( 'should not allow passing select option' , ( ) => {
176+ assertType < Parameters < QueryClient [ 'fetchInfiniteQuery' ] > > ( [
177+ {
178+ queryKey : [ 'key' ] ,
179+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
180+ initialPageParam : 1 ,
181+ getNextPageParam : ( ) => 2 ,
182+ // @ts -expect-error `select` is not supported on fetchInfiniteQuery options
183+ select : ( data ) => ( {
184+ pages : data . pages . map (
185+ ( x : unknown ) => `count: ${ ( x as { count : number } ) . count } ` ,
186+ ) ,
187+ pageParams : data . pageParams ,
188+ } ) ,
189+ } ,
190+ ] )
191+ } )
192+
161193 it ( 'should allow passing pages' , async ( ) => {
162194 const data = await new QueryClient ( ) . fetchInfiniteQuery ( {
163195 queryKey : [ 'key' ] ,
164- queryFn : ( ) => Promise . resolve ( 'string' ) ,
196+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
165197 getNextPageParam : ( ) => 1 ,
166198 initialPageParam : 1 ,
167199 pages : 5 ,
168200 } )
169201
170- expectTypeOf ( data ) . toEqualTypeOf < InfiniteData < string , number > > ( )
202+ expectTypeOf ( data ) . toEqualTypeOf < InfiniteData < { count : number } , number > > ( )
171203 } )
172204
173205 it ( 'should not allow passing getNextPageParam without pages' , ( ) => {
174206 assertType < Parameters < QueryClient [ 'fetchInfiniteQuery' ] > > ( [
175207 {
176208 queryKey : [ 'key' ] ,
177- queryFn : ( ) => Promise . resolve ( 'string' ) ,
209+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
178210 initialPageParam : 1 ,
179211 getNextPageParam : ( ) => 1 ,
180212 } ,
@@ -183,6 +215,72 @@ describe('fetchInfiniteQuery', () => {
183215
184216 it ( 'should not allow passing pages without getNextPageParam' , ( ) => {
185217 assertType < Parameters < QueryClient [ 'fetchInfiniteQuery' ] > > ( [
218+ // @ts -expect-error Property 'getNextPageParam' is missing
219+ {
220+ queryKey : [ 'key' ] ,
221+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
222+ initialPageParam : 1 ,
223+ pages : 5 ,
224+ } ,
225+ ] )
226+ } )
227+ } )
228+
229+ describe ( 'query' , ( ) => {
230+ it ( 'should allow passing select option' , ( ) => {
231+ assertType < Parameters < QueryClient [ 'query' ] > > ( [
232+ {
233+ queryKey : [ 'key' ] ,
234+ queryFn : ( ) => Promise . resolve ( 'string' ) ,
235+ select : ( data ) => ( data as string ) . length ,
236+ } ,
237+ ] )
238+ } )
239+ } )
240+
241+ describe ( 'infiniteQuery' , ( ) => {
242+ it ( 'should not allow passing select option' , ( ) => {
243+ assertType < Parameters < QueryClient [ 'infiniteQuery' ] > > ( [
244+ {
245+ queryKey : [ 'key' ] ,
246+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
247+ initialPageParam : 1 ,
248+ getNextPageParam : ( ) => 2 ,
249+ select : ( data ) => ( {
250+ pages : data . pages . map (
251+ ( x ) => `count: ${ ( x as { count : number } ) . count } ` ,
252+ ) ,
253+ pageParams : data . pageParams ,
254+ } ) ,
255+ } ,
256+ ] )
257+ } )
258+
259+ it ( 'should allow passing pages' , async ( ) => {
260+ const data = await new QueryClient ( ) . fetchInfiniteQuery ( {
261+ queryKey : [ 'key' ] ,
262+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
263+ getNextPageParam : ( ) => 1 ,
264+ initialPageParam : 1 ,
265+ pages : 5 ,
266+ } )
267+
268+ expectTypeOf ( data ) . toEqualTypeOf < InfiniteData < { count : number } , number > > ( )
269+ } )
270+
271+ it ( 'should not allow passing getNextPageParam without pages' , ( ) => {
272+ assertType < Parameters < QueryClient [ 'infiniteQuery' ] > > ( [
273+ {
274+ queryKey : [ 'key' ] ,
275+ queryFn : ( ) => Promise . resolve ( { count : 1 } ) ,
276+ initialPageParam : 1 ,
277+ getNextPageParam : ( ) => 1 ,
278+ } ,
279+ ] )
280+ } )
281+
282+ it ( 'should not allow passing pages without getNextPageParam' , ( ) => {
283+ assertType < Parameters < QueryClient [ 'infiniteQuery' ] > > ( [
186284 // @ts -expect-error Property 'getNextPageParam' is missing
187285 {
188286 queryKey : [ 'key' ] ,
@@ -227,6 +325,22 @@ describe('fully typed usage', () => {
227325 // Construct typed arguments
228326 //
229327
328+ const infiniteQueryOptions : InfiniteQueryExecuteOptions < TData , TError > = {
329+ queryKey : [ 'key' ] as any ,
330+ pages : 5 ,
331+ getNextPageParam : ( lastPage ) => {
332+ expectTypeOf ( lastPage ) . toEqualTypeOf < TData > ( )
333+ return 0
334+ } ,
335+ initialPageParam : 0 ,
336+ select : ( data ) => {
337+ expectTypeOf ( data ) . toEqualTypeOf < InfiniteData < TData , unknown > > ( )
338+ return data
339+ } ,
340+ }
341+
342+ const infiniteQueryOptions
343+
230344 const queryOptions : EnsureQueryDataOptions < TData , TError > = {
231345 queryKey : [ 'key' ] as any ,
232346 }
@@ -240,6 +354,7 @@ describe('fully typed usage', () => {
240354 } ,
241355 initialPageParam : 0 ,
242356 }
357+
243358 const mutationOptions : MutationOptions < TData , TError > = { }
244359
245360 const queryFilters : QueryFilters < DataTag < QueryKey , TData , TError > > = {
@@ -323,7 +438,12 @@ describe('fully typed usage', () => {
323438 > ( )
324439
325440 const infiniteQuery = await queryClient . infiniteQuery (
326- fetchInfiniteQueryOptions ,
441+ infiniteQueryOptions ,
442+ )
443+ expectTypeOf ( infiniteQuery ) . toEqualTypeOf < InfiniteData < TData , unknown > > ( )
444+
445+ const infiniteQuery = await queryClient . infiniteQuery (
446+ infiniteQueryOptions ,
327447 )
328448 expectTypeOf ( infiniteQuery ) . toEqualTypeOf < InfiniteData < TData , unknown > > ( )
329449
0 commit comments