Skip to content

Commit

Permalink
fix(store-sync): show TS error for non-existent tables when using zus…
Browse files Browse the repository at this point in the history
…tand (#1896)
  • Loading branch information
holic authored Nov 10, 2023
1 parent 140d362 commit 1327ea8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-points-mate.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 9 additions & 6 deletions packages/store-sync/src/zustand/syncToZustand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { createStore } from "./createStore";
import { createStorageAdapter } from "./createStorageAdapter";
import { Address } from "viem";

type AllTables<config extends StoreConfig, extraTables extends Tables> = ResolvedStoreConfig<config>["tables"] &
extraTables &
type AllTables<
config extends StoreConfig,
extraTables extends Tables | undefined
> = ResolvedStoreConfig<config>["tables"] &
(extraTables extends Tables ? extraTables : Record<never, never>) &
typeof storeTables &
typeof worldTables;

type SyncToZustandOptions<config extends StoreConfig, extraTables extends Tables> = SyncOptions & {
type SyncToZustandOptions<config extends StoreConfig, extraTables extends Tables | undefined> = SyncOptions & {
// require address for now to keep the data model + retrieval simpler
address: Address;
config: config;
Expand All @@ -20,13 +23,13 @@ type SyncToZustandOptions<config extends StoreConfig, extraTables extends Tables
startSync?: boolean;
};

type SyncToZustandResult<config extends StoreConfig, extraTables extends Tables> = SyncResult & {
type SyncToZustandResult<config extends StoreConfig, extraTables extends Tables | undefined> = SyncResult & {
tables: AllTables<config, extraTables>;
useStore: ZustandStore<AllTables<config, extraTables>>;
stopSync: () => void;
};

export async function syncToZustand<config extends StoreConfig, extraTables extends Tables>({
export async function syncToZustand<config extends StoreConfig, extraTables extends Tables | undefined>({
config,
tables: extraTables,
store,
Expand All @@ -41,7 +44,7 @@ export async function syncToZustand<config extends StoreConfig, extraTables exte
...extraTables,
...storeTables,
...worldTables,
} as AllTables<config, extraTables>;
} as unknown as AllTables<config, extraTables>;

const useStore = store ?? createStore({ tables });
const storageAdapter = createStorageAdapter({ store: useStore });
Expand Down

0 comments on commit 1327ea8

Please sign in to comment.