diff --git a/package.json b/package.json index fb810e97..75ad891d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rc-component/util", - "version": "1.3.0", + "version": "1.3.1", "description": "Common Utils For React Component", "keywords": [ "react", @@ -45,8 +45,8 @@ "@rc-component/father-plugin": "^2.0.1", "@rc-component/np": "^1.0.3", "@testing-library/react": "^16.0.0", - "@types/jest": "^29.4.0", - "@types/node": "^22.5.5", + "@types/jest": "^30.0.0", + "@types/node": "^24.6.1", "@types/react": "^18.0.0", "@types/react-dom": "^18.0.0", "@types/react-is": "^19.0.0", @@ -59,7 +59,7 @@ "eslint-plugin-jest": "^28.2.0", "eslint-plugin-unicorn": "^56.0.1", "father": "^4.1.3", - "glob": "^9.2.1", + "glob": "^11.0.3", "husky": "^9.1.6", "lint-staged": "^15.1.0", "prettier": "^3.3.2", diff --git a/src/hooks/useEvent.ts b/src/hooks/useEvent.ts index f9bb7dce..4c6f92a0 100644 --- a/src/hooks/useEvent.ts +++ b/src/hooks/useEvent.ts @@ -2,12 +2,18 @@ /* eslint-disable react-hooks/exhaustive-deps */ import * as React from 'react'; -function useEvent(callback: T): T { - const fnRef = React.useRef(); +function useEvent any) | undefined>( + callback: T, +): undefined extends T + ? ( + ...args: Parameters> + ) => ReturnType> | undefined + : T { + const fnRef = React.useRef(callback); fnRef.current = callback; - const memoFn = React.useCallback( - ((...args: any) => fnRef.current?.(...args)) as any, + const memoFn = React.useCallback( + (...args: any[]) => fnRef.current?.(...args), [], ); diff --git a/tests/hooks.test.tsx b/tests/hooks.test.tsx index b8154c66..fc096500 100644 --- a/tests/hooks.test.tsx +++ b/tests/hooks.test.tsx @@ -9,6 +9,7 @@ import useMobile from '../src/hooks/useMobile'; import useState from '../src/hooks/useState'; import useSyncState from '../src/hooks/useSyncState'; import useControlledState from '../src/hooks/useControlledState'; +import useEvent from '../src/hooks/useEvent'; global.disableUseId = false; @@ -706,4 +707,23 @@ describe('hooks', () => { expect(container.textContent).toEqual('2'); }); }); + + describe('useEvent', () => { + it('extract type', () => { + const Demo = (props: { + canUndefined?: (a: number) => boolean; + notUndefined: (a: number) => boolean; + }) => { + const ua = useEvent(props.canUndefined); + const ub = useEvent(props.notUndefined); + + ua(1); + ub(2); + + return null; + }; + + expect(Demo).toBeTruthy(); + }); + }); });