From 0efa6b1744cfc2138101da19b0488f5d11246f38 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 5 Jul 2023 11:27:20 +0200 Subject: [PATCH 1/3] add function overload for `useEventCallback` --- .../src/useEventCallback/useEventCallback.spec.ts | 10 ++++++++++ .../mui-utils/src/useEventCallback/useEventCallback.ts | 10 +++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/mui-utils/src/useEventCallback/useEventCallback.spec.ts b/packages/mui-utils/src/useEventCallback/useEventCallback.spec.ts index 84b87c47d77080..2a6fd93facea79 100644 --- a/packages/mui-utils/src/useEventCallback/useEventCallback.spec.ts +++ b/packages/mui-utils/src/useEventCallback/useEventCallback.spec.ts @@ -8,4 +8,14 @@ function InferenceTest() { useEventCallback((event: MouseEvent) => { expectType(event); }); + useEventCallback<[MouseEvent, number], number>((event, count) => { + expectType(event); + expectType(count); + return count; + }); + useEventCallback<(event: MouseEvent, count: number) => number>((event, count) => { + expectType(event); + expectType(count); + return count; + }); } diff --git a/packages/mui-utils/src/useEventCallback/useEventCallback.ts b/packages/mui-utils/src/useEventCallback/useEventCallback.ts index 7cc333c947125a..62e092f4f1dd20 100644 --- a/packages/mui-utils/src/useEventCallback/useEventCallback.ts +++ b/packages/mui-utils/src/useEventCallback/useEventCallback.ts @@ -5,7 +5,13 @@ import useEnhancedEffect from '../useEnhancedEffect'; /** * https://github.com/facebook/react/issues/14099#issuecomment-440013892 */ -export default function useEventCallback( +function useEventCallback any = (...args: unknown[]) => any>( + fn: T, +): T; +function useEventCallback( + fn: (...args: Args) => Return, +): (...args: Args) => Return; +function useEventCallback( fn: (...args: Args) => Return, ): (...args: Args) => Return { const ref = React.useRef(fn); @@ -20,3 +26,5 @@ export default function useEventCallback( [], ); } + +export default useEventCallback; From 258ded95985c54b5c6d961988f1a715162b6e065 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Jul 2023 16:50:18 +0200 Subject: [PATCH 2/3] rename generic --- packages/mui-utils/src/useEventCallback/useEventCallback.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/mui-utils/src/useEventCallback/useEventCallback.ts b/packages/mui-utils/src/useEventCallback/useEventCallback.ts index 62e092f4f1dd20..73573b46299d27 100644 --- a/packages/mui-utils/src/useEventCallback/useEventCallback.ts +++ b/packages/mui-utils/src/useEventCallback/useEventCallback.ts @@ -5,9 +5,9 @@ import useEnhancedEffect from '../useEnhancedEffect'; /** * https://github.com/facebook/react/issues/14099#issuecomment-440013892 */ -function useEventCallback any = (...args: unknown[]) => any>( - fn: T, -): T; +function useEventCallback any = (...args: unknown[]) => any>( + fn: Fn, +): Fn; function useEventCallback( fn: (...args: Args) => Return, ): (...args: Args) => Return; From d097a067da7d8ee24b5620bdd4c2320635f09aec Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 11 Jul 2023 16:50:48 +0200 Subject: [PATCH 3/3] change default return type to unknown --- packages/mui-utils/src/useEventCallback/useEventCallback.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mui-utils/src/useEventCallback/useEventCallback.ts b/packages/mui-utils/src/useEventCallback/useEventCallback.ts index 73573b46299d27..babd9a5efccfd4 100644 --- a/packages/mui-utils/src/useEventCallback/useEventCallback.ts +++ b/packages/mui-utils/src/useEventCallback/useEventCallback.ts @@ -5,7 +5,7 @@ import useEnhancedEffect from '../useEnhancedEffect'; /** * https://github.com/facebook/react/issues/14099#issuecomment-440013892 */ -function useEventCallback any = (...args: unknown[]) => any>( +function useEventCallback any = (...args: unknown[]) => unknown>( fn: Fn, ): Fn; function useEventCallback(