@@ -5,13 +5,16 @@ import {
55 createLiveQueryCollection ,
66 eq ,
77 gt ,
8+ parseLoadSubsetOptions ,
89} from "@tanstack/db"
910import { QueryClient } from "@tanstack/query-core"
1011import { z } from "zod"
1112import { queryCollectionOptions } from "../src/query"
13+ import type { QueryCollectionConfig } from "../src/query"
1214import type {
1315 DeleteMutationFnParams ,
1416 InsertMutationFnParams ,
17+ LoadSubsetOptions ,
1518 UpdateMutationFnParams ,
1619} from "@tanstack/db"
1720
@@ -403,4 +406,69 @@ describe(`Query collection type resolution tests`, () => {
403406 expectTypeOf ( selectUserData ) . parameters . toEqualTypeOf < [ ResponseType ] > ( )
404407 } )
405408 } )
409+
410+ describe ( `loadSubsetOptions type inference` , ( ) => {
411+ interface TestItem {
412+ id : string
413+ name : string
414+ }
415+
416+ it ( `should type loadSubsetOptions as LoadSubsetOptions in queryFn` , ( ) => {
417+ const config : QueryCollectionConfig < TestItem > = {
418+ id : `loadSubsetTest` ,
419+ queryClient,
420+ queryKey : [ `loadSubsetTest` ] ,
421+ queryFn : ( ctx ) => {
422+ // Verify that loadSubsetOptions is assignable to LoadSubsetOptions
423+ // This ensures it can be used where LoadSubsetOptions is expected
424+ expectTypeOf (
425+ ctx . meta ! . loadSubsetOptions
426+ ) . toExtend < LoadSubsetOptions > ( )
427+ // so that parseLoadSubsetOptions can be called without type errors
428+ parseLoadSubsetOptions ( ctx . meta ?. loadSubsetOptions )
429+ // The fact that this call compiles without errors verifies that
430+ // ctx.meta.loadSubsetOptions is typed correctly as LoadSubsetOptions
431+ return Promise . resolve ( [ ] )
432+ } ,
433+ getKey : ( item ) => item . id ,
434+ syncMode : `on-demand` ,
435+ }
436+
437+ const options = queryCollectionOptions ( config )
438+ createCollection ( options )
439+ } )
440+
441+ it ( `should allow meta to contain additional properties beyond loadSubsetOptions` , ( ) => {
442+ const config : QueryCollectionConfig < TestItem > = {
443+ id : `loadSubsetTest` ,
444+ queryClient,
445+ queryKey : [ `loadSubsetTest` ] ,
446+ queryFn : ( ctx ) => {
447+ // Verify that an object with loadSubsetOptions plus other properties
448+ // can be assigned to ctx.meta's type. This ensures the type is not too restrictive.
449+ const metaWithExtra = {
450+ loadSubsetOptions : ctx . meta ! . loadSubsetOptions ,
451+ customProperty : `test` ,
452+ anotherProperty : 123 ,
453+ }
454+
455+ // Test that this object can be assigned to ctx.meta's type
456+ // This verifies that ctx.meta allows additional properties beyond loadSubsetOptions
457+ const typedMeta : typeof ctx . meta = metaWithExtra
458+
459+ // Verify the assignment worked (this will fail at compile time if types don't match)
460+ expectTypeOf (
461+ typedMeta . loadSubsetOptions
462+ ) . toExtend < LoadSubsetOptions > ( )
463+
464+ return Promise . resolve ( [ ] )
465+ } ,
466+ getKey : ( item ) => item . id ,
467+ syncMode : `on-demand` ,
468+ }
469+
470+ const options = queryCollectionOptions ( config )
471+ createCollection ( options )
472+ } )
473+ } )
406474} )
0 commit comments