From 6010f51d0ae4224f256fdeb1f3c6d872a795954e Mon Sep 17 00:00:00 2001 From: Hill Date: Tue, 14 May 2024 17:35:44 +0800 Subject: [PATCH] fixed type script --- packages/reshow-flux/package.json | 2 +- .../src/__tests__/connectHookMoreTest.js | 2 +- packages/reshow-flux/src/index.js | 4 ++- packages/reshow-flux/src/useConnect.js | 24 ++++++++------- packages/reshow-flux/types/index.d.ts | 2 +- packages/reshow-flux/types/useConnect.d.ts | 27 ++++++++--------- packages/reshow-return/package.json | 2 +- packages/reshow-return/src/connectOptions.js | 21 +++++++------- .../reshow-return/src/ui/organisms/Return.jsx | 10 +++++-- packages/reshow-return/src/useClientReturn.js | 29 ++++++++++++------- packages/reshow-return/src/useReturn.js | 14 +++++++-- .../reshow-return/types/connectOptions.d.ts | 22 ++++++++------ .../types/ui/organisms/Return.d.ts | 9 ++++-- .../reshow-return/types/useClientReturn.d.ts | 14 +++++++-- packages/reshow-return/types/useReturn.d.ts | 16 +++++++--- packages/reshow-return/yarn.lock | 24 +++++++-------- 16 files changed, 138 insertions(+), 84 deletions(-) diff --git a/packages/reshow-flux/package.json b/packages/reshow-flux/package.json index a6a675fc..5a43b9d5 100644 --- a/packages/reshow-flux/package.json +++ b/packages/reshow-flux/package.json @@ -1,5 +1,5 @@ { - "version": "0.18.7", + "version": "0.18.8", "name": "reshow-flux", "repository": { "type": "git", diff --git a/packages/reshow-flux/src/__tests__/connectHookMoreTest.js b/packages/reshow-flux/src/__tests__/connectHookMoreTest.js index f359eeca..c0019017 100644 --- a/packages/reshow-flux/src/__tests__/connectHookMoreTest.js +++ b/packages/reshow-flux/src/__tests__/connectHookMoreTest.js @@ -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
; }; diff --git a/packages/reshow-flux/src/index.js b/packages/reshow-flux/src/index.js index 05f2f0cb..bb757e45 100644 --- a/packages/reshow-flux/src/index.js +++ b/packages/reshow-flux/src/index.js @@ -1,7 +1,9 @@ //@ts-check /** - * @typedef {import("./useConnect").CalculateStateCallback} CalculateStateCallback + * @template StateType + * + * @typedef {import("./useConnect").CalculateStateCallback} CalculateStateCallback */ /** diff --git a/packages/reshow-flux/src/useConnect.js b/packages/reshow-flux/src/useConnect.js index 3a55adee..a20f1693 100644 --- a/packages/reshow-flux/src/useConnect.js +++ b/packages/reshow-flux/src/useConnect.js @@ -30,10 +30,6 @@ const handleShouldComponentUpdate = ({ } }; -/** - * @typedef {Record} StateObject - */ - /** * @template StateType * @template ActionType @@ -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} calculateState * @property {boolean} [shouldComponentUpdate] * @property {function(any):StoreObject} [storeLocator] * @property {string} [displayName] @@ -74,8 +75,9 @@ const useConnect = /** * @template StateType * @template ActionType + * * @param {UseConnectOption} inputOptions - * @returns {GetStateCallback} + * @returns {GetStateCallback} */ (inputOptions) => (props) => { /** @@ -131,7 +133,7 @@ const useConnect = props.renewProps ? [props] : [] ); - return data.state || {}; + return /**@type StateType*/ (data.state || {}); }; export default useConnect; diff --git a/packages/reshow-flux/types/index.d.ts b/packages/reshow-flux/types/index.d.ts index 30fa06e8..c9bd783c 100644 --- a/packages/reshow-flux/types/index.d.ts +++ b/packages/reshow-flux/types/index.d.ts @@ -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 = import("./useConnect").CalculateStateCallback; export type StateMap = import("./ImmutableStore").StateMap; export type MaybeMapType = import("./ImmutableStore").MaybeMapType; export type ReducerTypeWithMap = import("./ImmutableStore").ReducerTypeWithMap; diff --git a/packages/reshow-flux/types/useConnect.d.ts b/packages/reshow-flux/types/useConnect.d.ts index 1bc78ea4..4536fa7f 100644 --- a/packages/reshow-flux/types/useConnect.d.ts +++ b/packages/reshow-flux/types/useConnect.d.ts @@ -1,20 +1,16 @@ export default useConnect; -export type StateObject = Record; export type StoreObject = import("reshow-flux-base").StoreObject; export type UseConnectWithStore = { store: StoreObject; }; -export type CalculateStateCallback = (prevState: StateObject, calculateOptions: any) => StateObject; -export type GetStateCallback = (props: StateObject) => StateObject; +export type CalculateStateCallback = (prevState: StateType | any, calculateOptions: any) => StateType; +export type GetStateCallback = (props: any) => StateType; export type UseConnectOption = { - calculateState: CalculateStateCallback; + calculateState: CalculateStateCallback; shouldComponentUpdate?: boolean; storeLocator?: (arg0: any) => StoreObject; displayName?: string; }; -/** - * @typedef {Record} StateObject - */ /** * @template StateType * @template ActionType @@ -27,23 +23,28 @@ export type UseConnectOption = { * @property {StoreObject} 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} calculateState * @property {boolean} [shouldComponentUpdate] * @property {function(any):StoreObject} [storeLocator] * @property {string} [displayName] */ -declare function useConnect(inputOptions: UseConnectOption): GetStateCallback; +declare function useConnect(inputOptions: UseConnectOption): GetStateCallback; diff --git a/packages/reshow-return/package.json b/packages/reshow-return/package.json index 231847bf..e4a108c9 100644 --- a/packages/reshow-return/package.json +++ b/packages/reshow-return/package.json @@ -1,5 +1,5 @@ { - "version": "0.18.0", + "version": "0.18.1", "name": "reshow-return", "repository": { "type": "git", diff --git a/packages/reshow-return/src/connectOptions.js b/packages/reshow-return/src/connectOptions.js index fdfe7034..05da0370 100644 --- a/packages/reshow-return/src/connectOptions.js +++ b/packages/reshow-return/src/connectOptions.js @@ -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 @@ -59,10 +58,6 @@ const stateValueGetter = * @typedef {Record} InitStateObject */ -/** - * @typedef {Record} StateObject - */ - /** * @typedef { string[] | InitStateObject } InitStatesType */ @@ -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} 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} calculateOptions + * @returns {StateType} */ const calculateState = (prevState, calculateOptions) => { /** @@ -160,7 +161,7 @@ const calculateState = (prevState, calculateOptions) => { } } - return bSame ? prevState : results; + return /**@type StateType*/ (bSame ? prevState : results); }; export default { diff --git a/packages/reshow-return/src/ui/organisms/Return.jsx b/packages/reshow-return/src/ui/organisms/Return.jsx index 5de39862..045d18cd 100644 --- a/packages/reshow-return/src/ui/organisms/Return.jsx +++ b/packages/reshow-return/src/ui/organisms/Return.jsx @@ -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} store * @property {import("../../connectOptions").InitStatesType} initStates * @property {{[key: string]: string[]}} [pathStates] * @property {string[]} [excludeStates] @@ -34,7 +37,10 @@ const getReturn = ({ } = {}) => { useConnect = useConnect || useConn({ ...connectOptions, ...options }); /** - * @param {ReturnProps} props + * @template StateType + * @template ActionType + * + * @param {ReturnProps} props */ const Return = (props) => { const { children, backfillProps, ...otherProps } = props; diff --git a/packages/reshow-return/src/useClientReturn.js b/packages/reshow-return/src/useClientReturn.js index 9c8a67ad..9d7f3fa4 100644 --- a/packages/reshow-return/src/useClientReturn.js +++ b/packages/reshow-return/src/useClientReturn.js @@ -4,19 +4,28 @@ import useReturn from "./useReturn"; import hydrate from "./hydrate"; /** - * @type {import('./useReturn').UseReturnType} + * @template StateType + * @template ActionType + * + * @typedef {import('./useReturn').UseReturnType} UseReturnType */ -const useClientReturn = (...p) => { - if (hydrate() || p[2]?.isHydrate) { - const state = useReturn(...p); + +/** + * @template StateType + * @template ActionType + * + * @type {UseReturnType} + */ +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); } }; diff --git a/packages/reshow-return/src/useReturn.js b/packages/reshow-return/src/useReturn.js index aa5d757d..77486957 100644 --- a/packages/reshow-return/src/useReturn.js +++ b/packages/reshow-return/src/useReturn.js @@ -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} store * @param {UseReturnPayLoad} [payload] + * + * @returns {StateType} */ /** - * @type UseReturnType + * @template StateType + * @template ActionType + * + * @type UseReturnType */ const useReturn = ( initStates, @@ -42,7 +50,7 @@ const useReturn = ( shouldComponentUpdate, immutable, }); - return state; + return /**@type StateType*/ (state); }; export default useReturn; diff --git a/packages/reshow-return/types/connectOptions.d.ts b/packages/reshow-return/types/connectOptions.d.ts index b000fcb0..b01b8e1e 100644 --- a/packages/reshow-return/types/connectOptions.d.ts +++ b/packages/reshow-return/types/connectOptions.d.ts @@ -5,14 +5,13 @@ declare namespace _default { } export default _default; export type InitStateObject = Record; -export type StateObject = Record; export type InitStatesType = string[] | InitStateObject; export type PathStates = { [x: string]: string[]; }; -export type calculateOptions = { +export type calculateOptions = { initStates: InitStatesType; - store: StoreObject; + store: import("reshow-flux-base").StoreObject; pathStates?: PathStates | undefined; excludeStates?: string[] | undefined; immutable?: boolean | undefined; @@ -21,23 +20,28 @@ export type calculateOptions = { * @typedef {Object} PathStates */ /** + * @template StateType + * @template ActionType + * * @typedef {object} calculateOptions * @property {InitStatesType} initStates - * @property {StoreObject} store + * @property {import("reshow-flux-base").StoreObject} 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} calculateOptions + * @returns {StateType} */ -declare function calculateState(prevState: StateObject, calculateOptions: calculateOptions): StateObject; +declare function calculateState(prevState: StateType, calculateOptions: calculateOptions): StateType; /** * @param {object} props * @param {object} [more] * @returns {object} */ declare function reset(props: object, more?: object): object; -import { StoreObject } from "reshow-flux-base"; diff --git a/packages/reshow-return/types/ui/organisms/Return.d.ts b/packages/reshow-return/types/ui/organisms/Return.d.ts index 0a51aa8a..6a949807 100644 --- a/packages/reshow-return/types/ui/organisms/Return.d.ts +++ b/packages/reshow-return/types/ui/organisms/Return.d.ts @@ -5,8 +5,8 @@ export type GetReturnOptions = { cleanProps?: string[]; options?: object; }; -export type ReturnProps = { - store: import("reshow-flux-base").StoreObject; +export type ReturnProps = { + store: import("reshow-flux-base").StoreObject; initStates: import("../../connectOptions").InitStatesType; pathStates?: { [key: string]: string[]; @@ -24,8 +24,11 @@ declare const Return: React.ElementType; * @property {object} [options] */ /** + * @template StateType + * @template ActionType + * * @typedef {object} ReturnProps - * @property {import("reshow-flux-base").StoreObject} store + * @property {import("reshow-flux-base").StoreObject} store * @property {import("../../connectOptions").InitStatesType} initStates * @property {{[key: string]: string[]}} [pathStates] * @property {string[]} [excludeStates] diff --git a/packages/reshow-return/types/useClientReturn.d.ts b/packages/reshow-return/types/useClientReturn.d.ts index b692cb68..4e6b0044 100644 --- a/packages/reshow-return/types/useClientReturn.d.ts +++ b/packages/reshow-return/types/useClientReturn.d.ts @@ -1,5 +1,15 @@ export default useClientReturn; +export type UseReturnType = import('./useReturn').UseReturnType; /** - * @type {import('./useReturn').UseReturnType} + * @template StateType + * @template ActionType + * + * @typedef {import('./useReturn').UseReturnType} UseReturnType */ -declare const useClientReturn: import('./useReturn').UseReturnType; +/** + * @template StateType + * @template ActionType + * + * @type {UseReturnType} + */ +declare const useClientReturn: UseReturnType; diff --git a/packages/reshow-return/types/useReturn.d.ts b/packages/reshow-return/types/useReturn.d.ts index 2a353491..b8f49a23 100644 --- a/packages/reshow-return/types/useReturn.d.ts +++ b/packages/reshow-return/types/useReturn.d.ts @@ -13,7 +13,7 @@ export type UseReturnPayLoad = { nextState: any; }) => boolean; }; -export type UseReturnType = (initStates: import('./connectOptions').InitStatesType, store: import("reshow-flux-base").StoreObject, payload?: UseReturnPayLoad) => any; +export type UseReturnType = (initStates: import('./connectOptions').InitStatesType, store: import("reshow-flux-base").StoreObject, payload?: UseReturnPayLoad) => StateType; /** * @typedef {object} UseReturnPayLoad * @property {{[key: string]: string[]}} [pathStates] @@ -24,12 +24,20 @@ export type UseReturnType = (initStates: import('./connectOptions').InitStatesTy * @property {function({prev:any, nextProps:any, nextState:any}):boolean} [shouldComponentUpdate] */ /** + * @template StateType + * @template ActionType + * * @callback UseReturnType * @param {import('./connectOptions').InitStatesType} initStates - * @param {import("reshow-flux-base").StoreObject} store + * @param {import("reshow-flux-base").StoreObject} store * @param {UseReturnPayLoad} [payload] + * + * @returns {StateType} */ /** - * @type UseReturnType + * @template StateType + * @template ActionType + * + * @type UseReturnType */ -declare const useReturn: UseReturnType; +declare const useReturn: UseReturnType; diff --git a/packages/reshow-return/yarn.lock b/packages/reshow-return/yarn.lock index 2b84d450..6cd9675a 100644 --- a/packages/reshow-return/yarn.lock +++ b/packages/reshow-return/yarn.lock @@ -868,9 +868,9 @@ camelcase@^6.0.0, camelcase@^6.3.0: integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== caniuse-lite@^1.0.30001587: - version "1.0.30001617" - resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001617.tgz#809bc25f3f5027ceb33142a7d6c40759d7a901eb" - integrity sha512-mLyjzNI9I+Pix8zwcrpxEbGlfqOkF9kM3ptzmKNw5tizSyYwMe+nGLTqMK9cO+0E+Bh6TsBxNAaHWEM8xwSsmA== + version "1.0.30001618" + resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001618.tgz#fad74fa006aef0f01e8e5c0a5540c74d8d36ec6f" + integrity sha512-p407+D1tIkDvsEAPS22lJxLQQaG8OTBEqo0KhzfABGk0TU4juBNDSfH0hyAp/HRyx+M8L17z/ltyhxh27FTfQg== chai@^4.x: version "4.4.1" @@ -1049,9 +1049,9 @@ convert-source-map@^2.0.0: integrity sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg== core-js@^3.24.1: - version "3.37.0" - resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.37.0.tgz#d8dde58e91d156b2547c19d8a4efd5c7f6c426bb" - integrity sha512-fu5vHevQ8ZG4og+LXug8ulUtVxjOcEYvifJr7L5Bfq9GOztVqsKd9/59hUk2ZSbCrS3BqUr3EpaYGIYzq7g3Ug== + version "3.37.1" + resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.37.1.tgz#d21751ddb756518ac5a00e4d66499df981a62db9" + integrity sha512-Xn6qmxrQZyB0FFY8E3bgRXei3lWDJHhvI+u0q9TKIYM49G8pAr0FgnnrFRAmsbptZL1yxRADVXn+x5AGsbBfyw== cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3: version "7.0.3" @@ -1222,9 +1222,9 @@ dom-accessibility-api@^0.5.9: integrity sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg== electron-to-chromium@^1.4.668: - version "1.4.766" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.766.tgz#09fa302a4d0201e6aafc4da815d6b40e93b75c8d" - integrity sha512-QkqagkSWWIngOO+f/DkMtTfzX/hpESMljeYzwZvOzmk2G6oEiG1JxE2hVXY6/XoVXMkILaJ6ASUnrMPiEA7x9A== + version "1.4.767" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.767.tgz#b885cfefda5a2e7a7ee356c567602012294ed260" + integrity sha512-nzzHfmQqBss7CE3apQHkHjXW77+8w3ubGCIoEijKCJebPufREaFETgGXWTkh32t259F3Kcq+R8MZdFdOJROgYw== emoji-regex@^7.0.1: version "7.0.3" @@ -2871,9 +2871,9 @@ reshow-flux-base@*: integrity sha512-n+PNqP274AjvcP6KipyvCIbHAB8mFdusXnKIbx45tb7sQnjXBgpx6STCAVv3N8qXRU97fEr1NapUKTf14NlTHg== reshow-flux@*: - version "0.18.7" - resolved "https://registry.yarnpkg.com/reshow-flux/-/reshow-flux-0.18.7.tgz#3b532450c61b05ae6a29d233b943fae16078c73f" - integrity sha512-BgmjvKDYuCMtcP9fnTZiCmq2Je0K/A68uxQBriNp/7RodXzWMmWaBbYS5cIlAob4hPPn/GZd/HU+ItUXrUe55Q== + version "0.18.8" + resolved "https://registry.yarnpkg.com/reshow-flux/-/reshow-flux-0.18.8.tgz#73a46bf7271a1779346c88078840ddfd6e6a50e9" + integrity sha512-W1GKQSr/CcNflIaXNnRCERx7U8YbVQLeB/kQGYGZn6g6bbaT+5+Qy+4Dys7w7ScXanKnVTicSG2Ait865f8sog== dependencies: array.dedup "*" array.merge "*"