Repository navigation
Closed
Labels
Description
Description
Currently, query-db-collection requires explicit type parameters or schema for type inference. This issue proposes adding support for inferring types from the queryFn return type, similar to how electric-db-collection uses its ResolveType system.
Current Behavior
Users must provide types in one of these ways:
// Explicit type parameter
const collection = createCollection<Todo>(queryCollectionOptions({...}))
// Schema inference
const collection = createCollection(queryCollectionOptions({
schema: todoSchema,
...
}))Proposed Enhancement
Add automatic type inference from queryFn when no explicit type or schema is provided:
// Type would be inferred from queryFn return type
const collection = createCollection(queryCollectionOptions({
queryFn: async () => {
const response = await fetch('/api/todos')
return response.json() as Todo[] // Type inferred from here
},
...
}))Implementation Details
1. Create Type Helper
type InferQueryFnOutput<TQueryFn> = TQueryFn extends (
context: QueryFunctionContext<any>
) => Promise<Array<infer TItem>>
? TItem
: never2. Add ResolveType System
type ResolveType<
TExplicit = unknown,
TSchema extends StandardSchemaV1 = never,
TQueryFn = unknown,
> =
unknown extends TExplicit
? [TSchema] extends [never]
? InferQueryFnOutput<TQueryFn> extends never
? Record<string, unknown> // fallback
: InferQueryFnOutput<TQueryFn>
: InferSchemaOutput<TSchema>
: TExplicit3. Update QueryCollectionConfig
Update the interface to use the new type resolution system, with priority:
- Explicit type (highest priority)
- Schema inference
- QueryFn return type inference
- Fallback to Record<string, unknown>
Benefits
- Better developer experience with automatic type inference
- Consistency with electric-db-collection's flexible type system
- Reduces boilerplate when types are already defined in queryFn
Testing Requirements
- Test type inference with various queryFn patterns
- Ensure backward compatibility with existing usage
- Verify priority order of type resolution
Related
- Similar to electric-db-collection's ResolveType implementation in packages/electric-db-collection/src/electric.ts:47-57
Reacted by Alec and TAEHO KIM
Activity
rawvv commentedon Aug 6, 2025
I'm interested in working on this issue as my first contribution! Any guidance on where to start would be appreciated 🙂
KyleAMathews commentedon Aug 6, 2025
Go ahead! The issue body has all of our current research on this.
feat: add type inference from queryFn return type
rawvv commentedon Aug 13, 2025
@KyleAMathews I've submitted PR #403 that implements the type inference from queryFn return type as described in this issue. Could you please review it when you have a chance? Thanks!
KyleAMathews commentedon Aug 13, 2025
I'm on parental leave atm — @kevin-dp or @samwillis could you help get this merged?
samwillis commentedon Aug 19, 2025
Thanks for this @ho8ae, PR was perfect. I added a changeset and merged it. It will be out in a release later today.
rawvv commentedon Aug 20, 2025
Thank you so much @samwillis! This was an amazing first contribution experience.
If you have any other interesting issues that could use some help, I'd love to tackle them!