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

useStore uses ReadonlyStoreApi #2586

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>
type ReadonlyStoreApi<T> = Pick<
StoreApi<T>,
'getState' | 'getInitialState' | 'subscribe'
>

type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
/** @deprecated please use api.getInitialState() */
Expand All @@ -31,11 +34,11 @@ let didWarnAboutEqualityFn = false

const identity = <T>(arg: T): T => arg

export function useStore<S extends WithReact<StoreApi<unknown>>>(
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>>(
api: S,
): ExtractState<S>

export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>, U>(
api: S,
selector: (state: ExtractState<S>) => U,
): U
Expand All @@ -44,14 +47,14 @@ export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
* @deprecated The usage with three arguments is deprecated. Use `useStoreWithEqualityFn` from 'zustand/traditional'. The usage with one or two arguments is not deprecated.
* https://github.com/pmndrs/zustand/discussions/1937
*/
export function useStore<S extends WithReact<StoreApi<unknown>>, U>(
export function useStore<S extends WithReact<ReadonlyStoreApi<unknown>>, U>(
api: S,
selector: (state: ExtractState<S>) => U,
equalityFn: ((a: U, b: U) => boolean) | undefined,
): U

export function useStore<TState, StateSlice>(
api: WithReact<StoreApi<TState>>,
api: WithReact<ReadonlyStoreApi<TState>>,
selector: (state: TState) => StateSlice = identity as any,
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
Expand Down
15 changes: 9 additions & 6 deletions src/traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports

type ExtractState<S> = S extends { getState: () => infer T } ? T : never

type ReadonlyStoreApi<T> = Pick<StoreApi<T>, 'getState' | 'subscribe'>
type ReadonlyStoreApi<T> = Pick<
StoreApi<T>,
'getState' | 'getInitialState' | 'subscribe'
>

type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {
/** @deprecated please use api.getInitialState() */
Expand All @@ -29,12 +32,12 @@ type WithReact<S extends ReadonlyStoreApi<unknown>> = S & {

const identity = <T>(arg: T): T => arg

export function useStoreWithEqualityFn<S extends WithReact<StoreApi<unknown>>>(
api: S,
): ExtractState<S>
export function useStoreWithEqualityFn<
S extends WithReact<ReadonlyStoreApi<unknown>>,
>(api: S): ExtractState<S>

export function useStoreWithEqualityFn<
S extends WithReact<StoreApi<unknown>>,
S extends WithReact<ReadonlyStoreApi<unknown>>,
U,
>(
api: S,
Expand All @@ -43,7 +46,7 @@ export function useStoreWithEqualityFn<
): U

export function useStoreWithEqualityFn<TState, StateSlice>(
api: WithReact<StoreApi<TState>>,
api: WithReact<ReadonlyStoreApi<TState>>,
selector: (state: TState) => StateSlice = identity as any,
equalityFn?: (a: StateSlice, b: StateSlice) => boolean,
) {
Expand Down
Loading