Skip to content

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

@merodiro

Description

@merodiro
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions