Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store factory typings #1100

Closed
Hrach-H opened this issue Jul 18, 2022 · 5 comments · Fixed by #1106
Closed

Store factory typings #1100

Hrach-H opened this issue Jul 18, 2022 · 5 comments · Fixed by #1106

Comments

@Hrach-H
Copy link

Hrach-H commented Jul 18, 2022

So I'm trying to create a store factory which will essentially adds just one helper method to the initial state object:

import create, { type State } from "zustand";
import { combine, persist } from "zustand/middleware";

export const createPreferencesStore = <TState extends State>(initialState: TState, storageKey = "preferences") =>
  create(
    persist(
      combine(initialState, (setState) => ({
        setPreference: <TKey extends keyof TState>(key: TKey, value: TState[TKey]) => {
          setState({ [key]: value } as unknown as Partial<TState>);
        },
      })),
      { name: storageKey }
    )
  );

Before the last update everything worked fine, but now I have an error: "TS4023: Exported variable 'createPreferencesStore' has or is using name 'StorePersist' from external module ".../node_modules/zustand/middleware/persist" but cannot be named." I understand that you have removed the StorePersist type from the exports? Is there any way to type the factory? I also think that the "as unknown as Partial" is not the correct way to type the function.

@dai-shi
Copy link
Member

dai-shi commented Jul 18, 2022

Thanks for reporting.
This is an unexpected regression with #1012.
I thought the refactor doesn't change anything, but it's not true. interfaces are hard for me.
Let me open a PR quickly.

@dai-shi
Copy link
Member

dai-shi commented Jul 18, 2022

Can you try #1101?
https://ci.codesandbox.io/status/pmndrs/zustand/pr/1101
Visit ☝️ and find "Local Install Instructions"

@Hrach-H
Copy link
Author

Hrach-H commented Jul 18, 2022

It is fixed!
Thank you for reacting so quickly :)

@dai-shi
Copy link
Member

dai-shi commented Jul 19, 2022

@Hrach-H
Copy link
Author

Hrach-H commented Jul 19, 2022

Sorry, completely forgot about it. Will try it out tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants