Created
January 18, 2025 02:27
-
-
Save huynhducduy/c977572c51b586cd12fccc309c13074e to your computer and use it in GitHub Desktop.
memoizedCallback.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'react' | |
declare module 'react' { | |
/* eslint-disable @typescript-eslint/no-explicit-any -- its intentional */ | |
/* | |
* Use this type to make sure that the callback passed will always be a memoized callback | |
* For any function type, wrap it with MemoizedCallback<T> | |
* Example: MemoizedCallback<(param: number) => boolean> | |
*/ | |
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type -- its intentional | |
interface MemoizedCallback<T extends Function | ((...args: any[]) => any)> { | |
(...args: Parameters<T>): ReturnType<T> | |
__memoized__: true | |
} | |
/* | |
* Use this type ONLY for functions with signature (arg: T) => void | |
* Example: MemoizedCallbackOrDispatch<string> for (arg: string) => void | |
*/ | |
type MemoizedCallbackOrDispatch<T> = | |
| MemoizedCallback<(arg: T) => void> | |
| Dispatch<SetStateAction<T>> | |
function useCallback<T extends (...args: any[]) => any>( | |
callback: T, | |
deps: any[], | |
): MemoizedCallback<T> | |
/* eslint-enable @typescript-eslint/no-explicit-any -- its intentional */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment