From a16bb8ffb90db5588fca38591ad393431fac2107 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 5 Jun 2024 16:12:31 +0100 Subject: [PATCH] useStore uses ReadonlyStoreApi --- src/react.ts | 13 ++++++++----- src/traditional.ts | 15 +++++++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/react.ts b/src/react.ts index e410fc2200..4471c9ce81 100644 --- a/src/react.ts +++ b/src/react.ts @@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports type ExtractState = S extends { getState: () => infer T } ? T : never -type ReadonlyStoreApi = Pick, 'getState' | 'subscribe'> +type ReadonlyStoreApi = Pick< + StoreApi, + 'getState' | 'getInitialState' | 'subscribe' +> type WithReact> = S & { /** @deprecated please use api.getInitialState() */ @@ -31,11 +34,11 @@ let didWarnAboutEqualityFn = false const identity = (arg: T): T => arg -export function useStore>>( +export function useStore>>( api: S, ): ExtractState -export function useStore>, U>( +export function useStore>, U>( api: S, selector: (state: ExtractState) => U, ): U @@ -44,14 +47,14 @@ export function useStore>, 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>, U>( +export function useStore>, U>( api: S, selector: (state: ExtractState) => U, equalityFn: ((a: U, b: U) => boolean) | undefined, ): U export function useStore( - api: WithReact>, + api: WithReact>, selector: (state: TState) => StateSlice = identity as any, equalityFn?: (a: StateSlice, b: StateSlice) => boolean, ) { diff --git a/src/traditional.ts b/src/traditional.ts index d22582b483..0d029bcc6c 100644 --- a/src/traditional.ts +++ b/src/traditional.ts @@ -20,7 +20,10 @@ const { useSyncExternalStoreWithSelector } = useSyncExternalStoreExports type ExtractState = S extends { getState: () => infer T } ? T : never -type ReadonlyStoreApi = Pick, 'getState' | 'subscribe'> +type ReadonlyStoreApi = Pick< + StoreApi, + 'getState' | 'getInitialState' | 'subscribe' +> type WithReact> = S & { /** @deprecated please use api.getInitialState() */ @@ -29,12 +32,12 @@ type WithReact> = S & { const identity = (arg: T): T => arg -export function useStoreWithEqualityFn>>( - api: S, -): ExtractState +export function useStoreWithEqualityFn< + S extends WithReact>, +>(api: S): ExtractState export function useStoreWithEqualityFn< - S extends WithReact>, + S extends WithReact>, U, >( api: S, @@ -43,7 +46,7 @@ export function useStoreWithEqualityFn< ): U export function useStoreWithEqualityFn( - api: WithReact>, + api: WithReact>, selector: (state: TState) => StateSlice = identity as any, equalityFn?: (a: StateSlice, b: StateSlice) => boolean, ) {