Skip to content

Commit

Permalink
improve type
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWangJustToDo committed Nov 26, 2024
1 parent 8dc3298 commit 8894f80
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
32 changes: 22 additions & 10 deletions packages/r-store/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export declare const useReactiveState: <T extends Record<string, unknown>>(initi
*/
export declare type UseSelectorWithState<T, C> = {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
/**
* @deprecated
* use `getReactiveState` / `getReadonlyState` instead
Expand All @@ -215,22 +215,28 @@ export declare type UseSelectorWithState<T, C> = {
getLifeCycle: () => LifeCycle;
getReactiveState: () => UnwrapNestedRefs<T>;
getReadonlyState: () => DeepReadonly<UnwrapNestedRefs<T>>;
/**
*
* @param selector - a method to select the state, when the state change, the `cb` will be called
* @param cb - a callback function
* @returns a unsubscribe function
*/
subscribe: <P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, cb?: () => void, shallow?: boolean) => () => void;
useDeepSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
};

Expand All @@ -239,7 +245,7 @@ export declare type UseSelectorWithState<T, C> = {
*/
export declare type UseSelectorWithStore<T> = {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
/**
* @deprecated
* use `getReactiveState` / `getReadonlyState` instead
Expand All @@ -248,22 +254,28 @@ export declare type UseSelectorWithStore<T> = {
getLifeCycle: () => LifeCycle;
getReactiveState: () => UnwrapNestedRefs<T>;
getReadonlyState: () => DeepReadonly<UnwrapNestedRefs<T>>;
/**
*
* @param selector - a method to select the state, when the state change, the `cb` will be called
* @param cb - a callback function
* @returns a unsubscribe function
*/
subscribe: <P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, cb?: () => void, shallow?: boolean) => () => void;
useShallowSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/r-store/src/__test__/type.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const useFf = createStore(() => {
return { vvv };
});

const h = useFf((s) => s);
const h = useFf((s) => s.vvv, (p, c) => p === c);

const i = useCount((s) => s);
const i = useCount((s) => s, Object.is);

const useCount_v2 = createState(
withActions(
Expand Down
4 changes: 2 additions & 2 deletions packages/r-store/src/shared/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export const createHook = <T extends Record<string, unknown>, C extends Record<s
const shallowStableHook = generateUseHook("shallow-stable");

function useSelector(): DeepReadonly<UnwrapNestedRefs<T>> & C;
function useSelector<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
function useSelector<P>(selector?: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean) {
function useSelector<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
function useSelector<P>(selector?: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean) {
return defaultHook(selector, compare);
}

Expand Down
16 changes: 11 additions & 5 deletions packages/r-store/src/state/createState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type Setup<T> = () => T;
*/
export type UseSelectorWithState<T, C> = {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
/**
* @deprecated
* use `getReactiveState` / `getReadonlyState` instead
Expand All @@ -29,22 +29,28 @@ export type UseSelectorWithState<T, C> = {
getLifeCycle: () => LifeCycle;
getReactiveState: () => UnwrapNestedRefs<T>;
getReadonlyState: () => DeepReadonly<UnwrapNestedRefs<T>>;
/**
*
* @param selector - a method to select the state, when the state change, the `cb` will be called
* @param cb - a callback function
* @returns a unsubscribe function
*/
subscribe: <P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, cb?: () => void, shallow?: boolean) => () => void;
useDeepSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>> & C;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: (prev: P, next: P) => boolean): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>> & C) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
};

Expand Down
18 changes: 12 additions & 6 deletions packages/r-store/src/store/createStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type { Creator } from "./_internal";
*/
export type UseSelectorWithStore<T> = {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
/**
* @deprecated
* use `getReactiveState` / `getReadonlyState` instead
Expand All @@ -20,23 +20,29 @@ export type UseSelectorWithStore<T> = {
getLifeCycle: () => LifeCycle;
getReactiveState: () => UnwrapNestedRefs<T>;
getReadonlyState: () => DeepReadonly<UnwrapNestedRefs<T>>;
/**
*
* @param selector - a method to select the state, when the state change, the `cb` will be called
* @param cb - a callback function
* @returns a unsubscribe function
*/
subscribe: <P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, cb?: () => void, shallow?: boolean) => () => void;
useShallowSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useShallowStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
useDeepStableSelector: {
(): DeepReadonly<UnwrapNestedRefs<T>>;
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P): P;
}
<P>(selector: (state: DeepReadonly<UnwrapNestedRefs<T>>) => P, compare?: <Q extends P = P>(prev: Q, next: Q) => boolean): P;
};
};

/**
Expand Down

0 comments on commit 8894f80

Please sign in to comment.