import { Dispatch, SetStateAction, useState } from 'react'; type SetStateFunction = Dispatch>; type SetStateParameters = Parameters>; type SetStateReturnType = ReturnType>; const useProtectedState = ( initialState: S | (() => S), protect: ( fn: SetStateFunction, ...args: SetStateParameters ) => SetStateReturnType, ): [S, SetStateFunction] => { const [state, setState] = useState(initialState); return [ state, (...args: SetStateParameters): SetStateReturnType => protect(setState, ...args), ]; }; export default useProtectedState;