type MemoizedFunction Promise | any> = (...args: Parameters) => Promise> | ReturnType; export function memoize Promise | any>(func: T): MemoizedFunction { const cache = new Map>(); return (...args: Parameters): Promise> | ReturnType => { const key = JSON.stringify(args); if (cache.has(key)) { return cache.get(key)!; } const result = func(...args); if (result instanceof Promise) { return result.then(resolved => { return resolved; }); } else { cache.set(key, result); return result; } }; }