Skip to content

createLiveQueryCollection generic type inference lost in 0.6 — useLiveQuery returns {} #1414

@odin-blockmind

Description

@odin-blockmind
  • I've validated the bug against the latest version of DB packages

Describe the bug

After upgrading from @tanstack/db@0.5.33 to @tanstack/db@0.6.0, useLiveQuery on a collection created with createLiveQueryCollection resolves data as {}[] instead of the expected row type. All property access on rows becomes a type error.

Additionally, persistedCollectionOptions from @tanstack/browser-db-sqlite-persistence erases the generic T when queryCollectionOptions() output is spread into it — T becomes object.

To Reproduce

  1. Create a collection with createCollection(queryCollectionOptions({schema: portfoliosSchema, ...}))
  2. Create a live query collection: createLiveQueryCollection((q) => q.from({p: portfoliosCollection}).where(...))
  3. Use it in a component: const { data: [portfolio] } = useLiveQuery(portfolioByIdQuery)
  4. Access portfolio.idTS2339: Property 'id' does not exist on type '{}'

Expected behavior

portfolio should be typed as the row type inferred from the collection's schema (e.g. Portfolio), preserving all field types from the Zod schema.

Minimal repro

import { createCollection, createLiveQueryCollection, eq } from '@tanstack/db'
import { queryCollectionOptions } from '@tanstack/query-db-collection'
import { useLiveQuery } from '@tanstack/react-db'
import { z } from 'zod'

const schema = z.object({ id: z.string(), name: z.string() })
type Item = z.infer<typeof schema>

const itemsCollection = createCollection(
  queryCollectionOptions({
    id: 'items',
    schema,
    getKey: (item) => item.id,
    queryKey: ['items'],
    queryFn: async () => [] as Item[],
    queryClient,
  }),
)

const itemByIdQuery = createLiveQueryCollection((q) =>
  q.from({ i: itemsCollection }).where(({ i }) => eq(i.id, 'some-id')),
)

// In component:
const { data: [item] } = useLiveQuery(itemByIdQuery)
item.id   // TS2339: Property 'id' does not exist on type '{}'
item.name // TS2339: Property 'name' does not exist on type '{}'

Works on: @tanstack/react-db@0.1.77 + @tanstack/db@0.5.33
Broken on: @tanstack/react-db@0.1.78 + @tanstack/db@0.6.0

Desktop:

  • OS: macOS 15.3 (arm64)
  • TypeScript: 5.9.3
  • Bun: 1.3.10
  • @tanstack/react-db: 0.1.78
  • @tanstack/db: 0.6.0
  • @tanstack/query-db-collection: 1.0.31

Additional context

Secondary issue: persistedCollectionOptions also erases generics when wrapping queryCollectionOptions via spread:

persistedCollectionOptions({
  persistence,
  schemaVersion: 1,
  ...queryCollectionOptions({ schema, getKey: (item) => item.id, ... })
  // getKey callback: TS2339: Property 'id' does not exist on type 'object'
})

TypeScript cannot infer T through the spread of another generic function's return.

Workaround: Pin to pre-0.6: @tanstack/react-db@0.1.77 + @tanstack/query-db-collection@1.0.30.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions