From 992be425d5a8f3cbbd2a03a45b0740664d378f05 Mon Sep 17 00:00:00 2001 From: duqingyu Date: Tue, 1 Nov 2022 18:49:29 +0800 Subject: [PATCH 1/2] fix(type): #10 fix Type checking error --- index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/index.ts b/index.ts index e7a7b6c..f822dd8 100644 --- a/index.ts +++ b/index.ts @@ -7,12 +7,13 @@ type ReadOnlyRefObject = { readonly current: T; }; +type UseStateRefValue = [T, Dispatch>, ReadOnlyRefObject]; type UseStateRef = { - (initialState: S | (() => S)): [S, Dispatch>, ReadOnlyRefObject]; - (): [S | undefined, Dispatch>, ReadOnlyRefObject]; + (initialState: S | (() => S)): UseStateRefValue; + (): UseStateRefValue; }; -const useStateRef: UseStateRef = (initialState?: S | (() => S)) => { +const useStateRef: UseStateRef = (initialState?: S | (() => S)): UseStateRefValue => { const [state, setState] = useState(initialState); const ref = useRef(state); From 5b19d05935a7bf7c5c5d2c9098c99a4665a30fc2 Mon Sep 17 00:00:00 2001 From: duqingyu <1065161421@qq.com> Date: Tue, 1 Nov 2022 18:54:05 +0800 Subject: [PATCH 2/2] fix(type): #10 fix Type checking error --- index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.ts b/index.ts index f822dd8..89deea1 100644 --- a/index.ts +++ b/index.ts @@ -13,7 +13,7 @@ type UseStateRef = { (): UseStateRefValue; }; -const useStateRef: UseStateRef = (initialState?: S | (() => S)): UseStateRefValue => { +const useStateRef: UseStateRef = (initialState?: S | (() => S)) => { const [state, setState] = useState(initialState); const ref = useRef(state); @@ -23,7 +23,7 @@ const useStateRef: UseStateRef = (initialState?: S | (() => S)): UseStateRefV setState(ref.current); }, []); - return [state, dispatch, ref]; + return [state, dispatch, ref] as UseStateRefValue; }; export = useStateRef;