Skip to content
Source
Sign inSign up
TanStack/db

persistedCollectionOptions: schema type parameter not inferred, result incompatible with createCollection #1452

Send to my agent
Closedmerodiro(GitHub profile, opens in a new tab) opened this issue 2 comments
  • I've validated the bug against the latest version of DB packages

Describe the bug

When passing a schema to persistedCollectionOptions, TypeScript fails to infer the TSchema type parameter. This causes two problems:

  1. Schema type not inferred: options.schema resolves to undefined instead of the actual schema type (e.g. a Zod schema). TSchema defaults to never.
  2. Incompatible with createCollection: The result cannot be passed to createCollection because:
    • createCollection's schema overloads require { schema: T } (required)
    • createCollection's no-schema overloads require { schema?: never }
    • The result has schema?: TSchema | undefined (optional), which matches neither

Root cause

persistedCollectionOptions lacks schema-aware overloads. Compare with localOnlyCollectionOptions (which works correctly) — it has separate overloads for the schema and no-schema cases with & { schema: T } / & { schema?: never } on both input and output types.

persistedCollectionOptions only has two overloads (sync-present vs sync-absent), neither of which distinguishes the schema case.

To Reproduce

import { z } from 'zod'
import { createCollection } from '@tanstack/db'
import { persistedCollectionOptions } from '@tanstack/db-sqlite-persistence-core'

const todoSchema = z.object({
  id: z.string(),
  title: z.string(),
})

const adapter = {
  loadSubset: () => Promise.resolve([]),
  applyCommittedTx: () => Promise.resolve(),
  ensureIndex: () => Promise.resolve(),
}

// TSchema defaults to `never` — schema type is lost
const options = persistedCollectionOptions({
  id: 'test',
  schema: todoSchema,
  schemaVersion: 1,
  getKey: (item) => item.id,
  persistence: { adapter },
})

// options.schema is `undefined` instead of `typeof todoSchema`

// This errors: "No overload matches this call"
const collection = createCollection(options)

Expected behavior

persistedCollectionOptions should infer the schema type and produce a result compatible with createCollection, matching the behavior of localOnlyCollectionOptions.

Additional context

The fix requires adding schema-aware overloads to persistedCollectionOptions (for both sync-present and sync-absent modes), following the same pattern used by localOnlyCollectionOptions in packages/db/src/local-only.ts.

  1. can we get this one merged soon :) i'm having the same issue and have to do some type gymnastics to get it working

  2. Fixed by #1866. That merged change adds schema-aware persistedCollectionOptions overloads for both sync-wrapped and local-only configurations, preserves the concrete schema on the returned options, and includes a type oracle covering composition with createCollection.

Sign in to join this conversation.