interface Result { res: T; } export function memoize(callback: () => T): () => T { let memo: null | Result = null; return (): T => { if (memo) { return memo.res; } const res = callback(); memo = { res }; return res; }; }