diff --git a/.changeset/seven-points-mate.md b/.changeset/seven-points-mate.md new file mode 100644 index 0000000000..d359485104 --- /dev/null +++ b/.changeset/seven-points-mate.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store-sync": patch +--- + +Fixed `syncToZustand` types so that non-existent tables give an error and `never` type instead of a generic `Table` type. diff --git a/packages/store-sync/src/zustand/syncToZustand.ts b/packages/store-sync/src/zustand/syncToZustand.ts index fe8d0950c4..7b88722863 100644 --- a/packages/store-sync/src/zustand/syncToZustand.ts +++ b/packages/store-sync/src/zustand/syncToZustand.ts @@ -6,12 +6,15 @@ import { createStore } from "./createStore"; import { createStorageAdapter } from "./createStorageAdapter"; import { Address } from "viem"; -type AllTables = ResolvedStoreConfig["tables"] & - extraTables & +type AllTables< + config extends StoreConfig, + extraTables extends Tables | undefined +> = ResolvedStoreConfig["tables"] & + (extraTables extends Tables ? extraTables : Record) & typeof storeTables & typeof worldTables; -type SyncToZustandOptions = SyncOptions & { +type SyncToZustandOptions = SyncOptions & { // require address for now to keep the data model + retrieval simpler address: Address; config: config; @@ -20,13 +23,13 @@ type SyncToZustandOptions = SyncResult & { +type SyncToZustandResult = SyncResult & { tables: AllTables; useStore: ZustandStore>; stopSync: () => void; }; -export async function syncToZustand({ +export async function syncToZustand({ config, tables: extraTables, store, @@ -41,7 +44,7 @@ export async function syncToZustand; + } as unknown as AllTables; const useStore = store ?? createStore({ tables }); const storageAdapter = createStorageAdapter({ store: useStore });