-
Notifications
You must be signed in to change notification settings - Fork 203
persistedCollectionOptions: schema type parameter not inferred, result incompatible with createCollection #1452
Description
- 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:
- Schema type not inferred:
options.schemaresolves toundefinedinstead of the actual schema type (e.g. a Zod schema).TSchemadefaults tonever. - Incompatible with
createCollection: The result cannot be passed tocreateCollectionbecause: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.