@@ -256,5 +256,62 @@ describe('typed query keys', () => {
256
256
// allows non typed too with loose types
257
257
queryCache . setQueryData ( DOCUMENTS_KEYS . byId ( '1' ) , { toto : true } )
258
258
} )
259
+
260
+ it ( 'supports optional params as extra query modifiers' , ( ) => {
261
+ const documentsListQuery = defineQueryOptions (
262
+ ( { page = 1 , withComments = false } : { page ?: number ; withComments ?: boolean } = { } ) => ( {
263
+ key : [ 'documents' , page , { comments : withComments } ] ,
264
+ query : async ( ) => ( {
265
+ page,
266
+ withComments,
267
+ list : [ ] as { id : string ; title : string ; hasComments : boolean } [ ] ,
268
+ } ) ,
269
+ } ) ,
270
+ )
271
+
272
+ const r0 = useQuery ( documentsListQuery ( ) )
273
+ expectTypeOf ( r0 . data . value ) . toEqualTypeOf <
274
+ | {
275
+ page : number
276
+ withComments : boolean
277
+ list : { id : string ; title : string ; hasComments : boolean } [ ]
278
+ }
279
+ | undefined
280
+ > ( )
281
+
282
+ // can also be not called
283
+ const r1 = useQuery ( documentsListQuery )
284
+ expectTypeOf ( r1 ) . toEqualTypeOf ( r0 )
285
+
286
+ const r2 = useQuery ( documentsListQuery , ( ) => ( { page : 2 , withComments : true } ) )
287
+ expectTypeOf ( r2 . data . value ) . toEqualTypeOf <
288
+ | {
289
+ page : number
290
+ withComments : boolean
291
+ list : { id : string ; title : string ; hasComments : boolean } [ ]
292
+ }
293
+ | undefined
294
+ > ( )
295
+
296
+ const queryCache = useQueryCache ( )
297
+ expectTypeOf (
298
+ queryCache . getQueryData ( documentsListQuery ( { page : 3 , withComments : true } ) . key ) ,
299
+ ) . toEqualTypeOf <
300
+ | {
301
+ page : number
302
+ withComments : boolean
303
+ list : { id : string ; title : string ; hasComments : boolean } [ ]
304
+ }
305
+ | undefined
306
+ > ( )
307
+ expectTypeOf ( queryCache . getQueryData ( documentsListQuery ( ) . key ) ) . toEqualTypeOf <
308
+ | {
309
+ page : number
310
+ withComments : boolean
311
+ list : { id : string ; title : string ; hasComments : boolean } [ ]
312
+ }
313
+ | undefined
314
+ > ( )
315
+ } )
259
316
} )
260
317
} )
0 commit comments