Skip to content

Commit

Permalink
feat: refine types for memorized
Browse files Browse the repository at this point in the history
  • Loading branch information
upupming authored and harttle committed Jun 14, 2021
1 parent 4e35280 commit 68ca781
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions functional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export function create3DArray<T> (N: number, M: number, L: number, n: T): T[][][
return [...Array(N)].map(() => create2DArray(M, L, n))
}

export function memorized (fn: Function, getKey = (...args: any[]) => args.join(',')): Function {
export function memorized <Args extends any[], T> (fn: (...args: Args) => T, getKey = (...args: any[]) => args.join(',')): (...args: Parameters<typeof fn>) => T {
const memo = new Map<string, any>()
return function (...args: any[]) {
return function (...args: Parameters<typeof fn>): T {
const key = getKey(...args)
if (!memo.has(key)) {
memo.set(key, fn(...args))
Expand Down

0 comments on commit 68ca781

Please sign in to comment.