From 7d8dcbdd8847902f3c3db7a492dca240fe2967b2 Mon Sep 17 00:00:00 2001 From: KamyarTaher <61198701+KamyarTaher@users.noreply.github.com> Date: Sat, 27 Jul 2024 18:02:54 +0200 Subject: [PATCH] fix bug --- src/core/index.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index 10fc4b7..282267b 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -1,6 +1,11 @@ import { create, StateCreator } from "zustand"; import { useShallow } from "zustand/react/shallow"; -import { persist, createJSONStorage, PersistOptions } from "zustand/middleware"; +import { + persist, + createJSONStorage, + PersistOptions, + StorageValue, +} from "zustand/middleware"; import { getNestedValue, setNestedValue, @@ -9,7 +14,6 @@ import { stableHash, deepMerge, } from "./utils"; -import { createPersister } from "./persistConfig"; import { loggingMiddleware } from "../middleware"; import { devToolsPlugin } from "../plugins"; import type { @@ -92,24 +96,29 @@ export function createStore( } if (persistOption) { - const persistConfig: PersistOptions, unknown> = { + type PersistState = Store; + type PersistedState = StorageValue; + + const persistConfig: PersistOptions = { name: storageName, storage: createJSONStorage(() => localStorage), }; if (typeof persistOption === "object") { - (persistConfig as any).partialize = (state: Store) => + persistConfig.partialize = (state: PersistState): PersistedState => Object.fromEntries( Object.entries(persistOption) .filter(([_, v]) => v) - .map(([k]) => [k, state[k as keyof T]]) - ) as Partial; + .map(([k]) => [k, state[k as keyof PersistState]]) + ) as PersistedState; } else if (typeof persistOption === "function") { - (persistConfig as any).partialize = persistOption; + persistConfig.partialize = persistOption as ( + state: PersistState + ) => PersistedState; } - finalStoreCreator = persist( - finalStoreCreator as StateCreator, [], []>, + finalStoreCreator = persist( + finalStoreCreator as StateCreator, persistConfig ); }