Skip to content

Instantly share code, notes, and snippets.

@jahilldev
Created July 8, 2022 11:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Fully typed Memoize function, maintains argument function and expected return types
function memoize<T extends (...args: any[]) => any>(
func: T
): (...funcArgs: Parameters<T>) => ReturnType<T> {
const results = {};
return (...args: Parameters<T>): ReturnType<T> => {
const cacheKey = JSON.stringify(args);
if (!results[cacheKey]) {
results[cacheKey] = func(...args);
}
return results[cacheKey];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment