Skip to content

Commit

Permalink
fixed type script
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed May 14, 2024
1 parent d68fa19 commit 6010f51
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 84 deletions.
2 changes: 1 addition & 1 deletion packages/reshow-flux/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.18.7",
"version": "0.18.8",
"name": "reshow-flux",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/reshow-flux/src/__tests__/connectHookMoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe("Test Connect hook for more test", () => {
const FakeComponent = (/**@type any*/ props) => {
const state = useConnect({
storeLocator: () => store,
calculateState: () => ({}),
calculateState: () => ({ foo: null }),
})(props);
return <div role="udom" data-foo={state.foo} data-bar={props.bar} />;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/reshow-flux/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//@ts-check

/**
* @typedef {import("./useConnect").CalculateStateCallback} CalculateStateCallback
* @template StateType
*
* @typedef {import("./useConnect").CalculateStateCallback<StateType>} CalculateStateCallback
*/

/**
Expand Down
24 changes: 13 additions & 11 deletions packages/reshow-flux/src/useConnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ const handleShouldComponentUpdate = ({
}
};

/**
* @typedef {Record<string, any>} StateObject
*/

/**
* @template StateType
* @template ActionType
Expand All @@ -48,23 +44,28 @@ const handleShouldComponentUpdate = ({
*/

/**
* @template StateType
*
* @callback CalculateStateCallback
* @param {StateObject} prevState
* @param {StateType|any} prevState
* @param {any} calculateOptions
* @returns {StateObject}
* @returns {StateType}
*/

/**
* @template StateType
*
* @callback GetStateCallback
* @param {StateObject} props
* @returns {StateObject}
* @param {any} props
* @returns {StateType}
*/

/**
* @template StateType
* @template ActionType
*
* @typedef {object} UseConnectOption
* @property {CalculateStateCallback} calculateState
* @property {CalculateStateCallback<StateType>} calculateState
* @property {boolean} [shouldComponentUpdate]
* @property {function(any):StoreObject<StateType, ActionType>} [storeLocator]
* @property {string} [displayName]
Expand All @@ -74,8 +75,9 @@ const useConnect =
/**
* @template StateType
* @template ActionType
*
* @param {UseConnectOption<StateType, ActionType>} inputOptions
* @returns {GetStateCallback}
* @returns {GetStateCallback<StateType>}
*/
(inputOptions) => (props) => {
/**
Expand Down Expand Up @@ -131,7 +133,7 @@ const useConnect =
props.renewProps ? [props] : []
);

return data.state || {};
return /**@type StateType*/ (data.state || {});
};

export default useConnect;
2 changes: 1 addition & 1 deletion packages/reshow-flux/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { default as useImmutable } from "./useImmutable";
export { default as useReduceStore } from "./useReduceStore";
export { default as useStore } from "./useStore";
export { default as toJS } from "./toJS";
export type CalculateStateCallback = import("./useConnect").CalculateStateCallback;
export type CalculateStateCallback<StateType> = import("./useConnect").CalculateStateCallback<StateType>;
export type StateMap = import("./ImmutableStore").StateMap;
export type MaybeMapType = import("./ImmutableStore").MaybeMapType;
export type ReducerTypeWithMap<StateType = import("./ImmutableStore").StateMap, ActionType = import("./ImmutableStore").MaybeMapType> = import("./ImmutableStore").ReducerTypeWithMap<StateType, ActionType>;
Expand Down
27 changes: 14 additions & 13 deletions packages/reshow-flux/types/useConnect.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
export default useConnect;
export type StateObject = Record<string, any>;
export type StoreObject<StateType, ActionType> = import("reshow-flux-base").StoreObject<StateType, ActionType>;
export type UseConnectWithStore<StateType, ActionType> = {
store: StoreObject<StateType, ActionType>;
};
export type CalculateStateCallback = (prevState: StateObject, calculateOptions: any) => StateObject;
export type GetStateCallback = (props: StateObject) => StateObject;
export type CalculateStateCallback<StateType> = (prevState: StateType | any, calculateOptions: any) => StateType;
export type GetStateCallback<StateType> = (props: any) => StateType;
export type UseConnectOption<StateType, ActionType> = {
calculateState: CalculateStateCallback;
calculateState: CalculateStateCallback<StateType>;
shouldComponentUpdate?: boolean;
storeLocator?: (arg0: any) => StoreObject<StateType, ActionType>;
displayName?: string;
};
/**
* @typedef {Record<string, any>} StateObject
*/
/**
* @template StateType
* @template ActionType
Expand All @@ -27,23 +23,28 @@ export type UseConnectOption<StateType, ActionType> = {
* @property {StoreObject<StateType, ActionType>} store
*/
/**
* @template StateType
*
* @callback CalculateStateCallback
* @param {StateObject} prevState
* @param {StateType|any} prevState
* @param {any} calculateOptions
* @returns {StateObject}
* @returns {StateType}
*/
/**
* @template StateType
*
* @callback GetStateCallback
* @param {StateObject} props
* @returns {StateObject}
* @param {any} props
* @returns {StateType}
*/
/**
* @template StateType
* @template ActionType
*
* @typedef {object} UseConnectOption
* @property {CalculateStateCallback} calculateState
* @property {CalculateStateCallback<StateType>} calculateState
* @property {boolean} [shouldComponentUpdate]
* @property {function(any):StoreObject<StateType, ActionType>} [storeLocator]
* @property {string} [displayName]
*/
declare function useConnect<StateType, ActionType>(inputOptions: UseConnectOption<StateType, ActionType>): GetStateCallback;
declare function useConnect<StateType, ActionType>(inputOptions: UseConnectOption<StateType, ActionType>): GetStateCallback<StateType>;
2 changes: 1 addition & 1 deletion packages/reshow-return/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.18.0",
"version": "0.18.1",
"name": "reshow-return",
"repository": {
"type": "git",
Expand Down
21 changes: 11 additions & 10 deletions packages/reshow-return/src/connectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import get from "get-object-value";
import { toJS } from "reshow-flux";
import { IS_ARRAY, KEYS, T_UNDEFINED } from "reshow-constant";
import { StoreObject } from "reshow-flux-base";

/**
* @param {object} props
Expand Down Expand Up @@ -59,10 +58,6 @@ const stateValueGetter =
* @typedef {Record<string, string>} InitStateObject
*/

/**
* @typedef {Record<string, any>} StateObject
*/

/**
* @typedef { string[] | InitStateObject } InitStatesType
*/
Expand Down Expand Up @@ -96,18 +91,24 @@ const getImmutable = (immutable) => (data) => !immutable ? toJS(data) : data;
*/

/**
* @template StateType
* @template ActionType
*
* @typedef {object} calculateOptions
* @property {InitStatesType} initStates
* @property {StoreObject} store
* @property {import("reshow-flux-base").StoreObject<StateType, ActionType>} store
* @property {PathStates=} pathStates
* @property {string[]=} excludeStates
* @property {boolean=} immutable
*/

/**
* @param {StateObject} prevState
* @param {calculateOptions} calculateOptions
* @returns {StateObject}
* @template StateType
* @template ActionType
*
* @param {StateType} prevState
* @param {calculateOptions<StateType, ActionType>} calculateOptions
* @returns {StateType}
*/
const calculateState = (prevState, calculateOptions) => {
/**
Expand Down Expand Up @@ -160,7 +161,7 @@ const calculateState = (prevState, calculateOptions) => {
}
}

return bSame ? prevState : results;
return /**@type StateType*/ (bSame ? prevState : results);
};

export default {
Expand Down
10 changes: 8 additions & 2 deletions packages/reshow-return/src/ui/organisms/Return.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import * as React from "react";
*/

/**
* @template StateType
* @template ActionType
*
* @typedef {object} ReturnProps
* @property {import("reshow-flux-base").StoreObject} store
* @property {import("reshow-flux-base").StoreObject<StateType, ActionType>} store
* @property {import("../../connectOptions").InitStatesType} initStates
* @property {{[key: string]: string[]}} [pathStates]
* @property {string[]} [excludeStates]
Expand All @@ -34,7 +37,10 @@ const getReturn = ({
} = {}) => {
useConnect = useConnect || useConn({ ...connectOptions, ...options });
/**
* @param {ReturnProps} props
* @template StateType
* @template ActionType
*
* @param {ReturnProps<StateType, ActionType>} props
*/
const Return = (props) => {
const { children, backfillProps, ...otherProps } = props;
Expand Down
29 changes: 19 additions & 10 deletions packages/reshow-return/src/useClientReturn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ import useReturn from "./useReturn";
import hydrate from "./hydrate";

/**
* @type {import('./useReturn').UseReturnType}
* @template StateType
* @template ActionType
*
* @typedef {import('./useReturn').UseReturnType<StateType, ActionType>} UseReturnType
*/
const useClientReturn = (...p) => {
if (hydrate() || p[2]?.isHydrate) {
const state = useReturn(...p);

/**
* @template StateType
* @template ActionType
*
* @type {UseReturnType<StateType, ActionType>}
*/
const useClientReturn = (p1, p2, p3) => {
if (hydrate() || p3?.isHydrate) {
const p2Any = /**@type any*/ (p2);
const state = useReturn(p1, p2Any, p3);
const isLoad = useLoaded();
if (isLoad) {
return state;
} else {
return {};
}
return /**@type StateType*/ (isLoad ? state : {});
} else {
return useReturn(...p);
const p2Any = /**@type any*/ (p2);
const anyState = /**@type any*/ (useReturn(p1, p2Any, p3));
return /**@type StateType*/ (anyState);
}
};

Expand Down
14 changes: 11 additions & 3 deletions packages/reshow-return/src/useReturn.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ import connectOptions from "./connectOptions";
*/

/**
* @template StateType
* @template ActionType
*
* @callback UseReturnType
* @param {import('./connectOptions').InitStatesType} initStates
* @param {import("reshow-flux-base").StoreObject} store
* @param {import("reshow-flux-base").StoreObject<StateType, ActionType>} store
* @param {UseReturnPayLoad} [payload]
*
* @returns {StateType}
*/

/**
* @type UseReturnType
* @template StateType
* @template ActionType
*
* @type UseReturnType<StateType, ActionType>
*/
const useReturn = (
initStates,
Expand All @@ -42,7 +50,7 @@ const useReturn = (
shouldComponentUpdate,
immutable,
});
return state;
return /**@type StateType*/ (state);
};

export default useReturn;
22 changes: 13 additions & 9 deletions packages/reshow-return/types/connectOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ declare namespace _default {
}
export default _default;
export type InitStateObject = Record<string, string>;
export type StateObject = Record<string, any>;
export type InitStatesType = string[] | InitStateObject;
export type PathStates = {
[x: string]: string[];
};
export type calculateOptions = {
export type calculateOptions<StateType, ActionType> = {
initStates: InitStatesType;
store: StoreObject<any, any>;
store: import("reshow-flux-base").StoreObject<StateType, ActionType>;
pathStates?: PathStates | undefined;
excludeStates?: string[] | undefined;
immutable?: boolean | undefined;
Expand All @@ -21,23 +20,28 @@ export type calculateOptions = {
* @typedef {Object<string, string[]>} PathStates
*/
/**
* @template StateType
* @template ActionType
*
* @typedef {object} calculateOptions
* @property {InitStatesType} initStates
* @property {StoreObject} store
* @property {import("reshow-flux-base").StoreObject<StateType, ActionType>} store
* @property {PathStates=} pathStates
* @property {string[]=} excludeStates
* @property {boolean=} immutable
*/
/**
* @param {StateObject} prevState
* @param {calculateOptions} calculateOptions
* @returns {StateObject}
* @template StateType
* @template ActionType
*
* @param {StateType} prevState
* @param {calculateOptions<StateType, ActionType>} calculateOptions
* @returns {StateType}
*/
declare function calculateState(prevState: StateObject, calculateOptions: calculateOptions): StateObject;
declare function calculateState<StateType, ActionType>(prevState: StateType, calculateOptions: calculateOptions<StateType, ActionType>): StateType;
/**
* @param {object} props
* @param {object} [more]
* @returns {object}
*/
declare function reset(props: object, more?: object): object;
import { StoreObject } from "reshow-flux-base";
9 changes: 6 additions & 3 deletions packages/reshow-return/types/ui/organisms/Return.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export type GetReturnOptions = {
cleanProps?: string[];
options?: object;
};
export type ReturnProps = {
store: import("reshow-flux-base").StoreObject<any, any>;
export type ReturnProps<StateType, ActionType> = {
store: import("reshow-flux-base").StoreObject<StateType, ActionType>;
initStates: import("../../connectOptions").InitStatesType;
pathStates?: {
[key: string]: string[];
Expand All @@ -24,8 +24,11 @@ declare const Return: React.ElementType<any, keyof React.JSX.IntrinsicElements>;
* @property {object} [options]
*/
/**
* @template StateType
* @template ActionType
*
* @typedef {object} ReturnProps
* @property {import("reshow-flux-base").StoreObject} store
* @property {import("reshow-flux-base").StoreObject<StateType, ActionType>} store
* @property {import("../../connectOptions").InitStatesType} initStates
* @property {{[key: string]: string[]}} [pathStates]
* @property {string[]} [excludeStates]
Expand Down
Loading

0 comments on commit 6010f51

Please sign in to comment.