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

Error when using global store with Typescript #23

Open
IamMrandrew opened this issue Jun 12, 2024 · 1 comment
Open

Error when using global store with Typescript #23

IamMrandrew opened this issue Jun 12, 2024 · 1 comment

Comments

@IamMrandrew
Copy link

IamMrandrew commented Jun 12, 2024

I am trying to have a global store that can be retrieve inside lens for my store.

"@dhmk/zustand-lens": "^4.0.0",
"zustand": "^4.5.2"

Here's my code.

interface Store {
  auth: AuthSlice;
  sidebar: SidebarSlice;
  loading: boolean;
}

export const useStore = create<Store>()(
  devtools(
    withLenses(() => ({
      auth: lens<AuthSlice>(createAuthSlice),
      sidebar: lens<SidebarSlice>(createSidebarSlice),
      loading: false,
    })),
  )
);

So that in my slices, I can access the store by:

(set, get, store) => ({
  // ...
  store.setState({ loading: false });
})

There's no error popping up in my IDE, but get this error when starting react.

ERROR in src/stores/store.ts:15:16
TS2345: Argument of type '() => { auth: LensOpaqueType<AuthSlice, unknown>; sidebar: LensOpaqueType<SidebarSlice, unknown>; loading: boolean; }' is not assignable to parameter of type 'CheckLenses<{ auth: AuthSlice & LensTypeInfo<AuthSlice, StoreApi<unknown>>; sidebar: SidebarSlice & LensTypeInfo<SidebarSlice, StoreApi<...>>; loading: boolean; } & LensMeta<...>, WithDevtools<...>> | StateCreator<...>'.
  Type '() => { auth: LensOpaqueType<AuthSlice, unknown>; sidebar: LensOpaqueType<SidebarSlice, unknown>; loading: boolean; }' is not assignable to type 'StateCreator<{ auth: AuthSlice & LensTypeInfo<AuthSlice, StoreApi<unknown>>; sidebar: SidebarSlice & LensTypeInfo<SidebarSlice, StoreApi<...>>; loading: boolean; }, [...], [], CheckLenses<...>>'.
    Type '() => { auth: LensOpaqueType<AuthSlice, unknown>; sidebar: LensOpaqueType<SidebarSlice, unknown>; loading: boolean; }' is not assignable to type '(setState: <A extends string | { type: string; }>(partial: { auth: AuthSlice & LensTypeInfo<AuthSlice, StoreApi<unknown>>; sidebar: SidebarSlice & LensTypeInfo<...>; loading: boolean; } | Partial<...> | ((state: { ...; }) => { ...; } | Partial<...>), replace?: boolean, action?: A) => void, getState: () => { ...; }, ...'.
      Type '{ auth: LensOpaqueType<AuthSlice, unknown>; sidebar: LensOpaqueType<SidebarSlice, unknown>; loading: boolean; }' is not assignable to type 'CheckLenses<{ auth: AuthSlice & LensTypeInfo<AuthSlice, StoreApi<unknown>>; sidebar: SidebarSlice & LensTypeInfo<SidebarSlice, StoreApi<...>>; loading: boolean; } & LensMeta<...>, WithDevtools<...>>'.
        Property 'loading' is incompatible with index signature.
          Type 'boolean' has no properties in common with type 'LensTypeInfo<unknown, StoreApi<unknown>>'.
    13 | export const useStore = create<Store>()(
    14 |   devtools(
  > 15 |     withLenses(() => ({
       |                ^^^^^^^^
  > 16 |       auth: lens<AuthSlice>(createAuthSlice),
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 17 |       sidebar: lens<SidebarSlice>(createSidebarSlice),
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 18 |       loading: false,
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  > 19 |     })),
       | ^^^^^^^
    20 |   )
    21 | );
    22 |
@dhmk083
Copy link
Owner

dhmk083 commented Jun 13, 2024

It works fine for me. Could you provide a full example?

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

No branches or pull requests

2 participants