Skip to content

Commit

Permalink
useStore uses ReadonlyStoreApi (#2586)
Browse files Browse the repository at this point in the history
Co-authored-by: Daishi Kato <[email protected]>
  • Loading branch information
holic and dai-shi authored Jun 26, 2024
1 parent 7e15364 commit a262c9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
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

0 comments on commit a262c9f

Please sign in to comment.