Skip to content

Instantly share code, notes, and snippets.

@huynhducduy
Created January 18, 2025 02:27
Show Gist options
  • Save huynhducduy/c977572c51b586cd12fccc309c13074e to your computer and use it in GitHub Desktop.
Save huynhducduy/c977572c51b586cd12fccc309c13074e to your computer and use it in GitHub Desktop.
memoizedCallback.d.ts
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