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

Aperta Adatta ai principianti
#1,452 1 commento 8 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
typescript
Ambito
api

Direzione di ricerca

Inizia da persistedCollectionOptions e confronta i suoi overload con i pattern consapevoli dello schema in packages/db/src/local-only.ts. Verifica che sia nei casi con sync presente sia in quelli con sync assente venga preservato il tipo dello schema e che le opzioni restituite possano essere passate a createCollection; il lavoro è completato quando la riproduzione inferisce lo schema e supera il controllo dei tipi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
TypeScript
Stelle
3.9k
Fork
266
Merge medio
1g 4h
PR unite (30g)
52

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/db

Tutte le issue di TanStack/db

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.