Skip to content

Commit

Permalink
fix memo typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Epps authored and Ray Epps committed Feb 21, 2024
1 parent fb1a841 commit f9bc525
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/curry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,17 +466,16 @@ const memoize = <TArgs extends any[], TResult>(
* is given previously computed values will be checked
* for expiration before being returned.
*/
export const memo = <
TArgs extends any[],
TFunc extends (...args: TArgs) => any
>(
func: TFunc,
export const memo = <TArgs extends any[]>(
func: (...args: TArgs) => any,
options: {
key?: (...args: TArgs) => string
ttl?: number
} = {}
) => {
return memoize({}, func, options.key ?? null, options.ttl ?? null) as TFunc
return memoize({}, func, options.key ?? null, options.ttl ?? null) as (
...args: TArgs
) => any
}

export type DebounceFunction<TArgs extends any[]> = {
Expand Down
12 changes: 6 additions & 6 deletions src/tests/curry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,17 @@ describe('curry module', () => {
})
test('uses key to identify unique calls', () => {
const func = _.memo(
({ id }: { id: string }) => {
(arg: { user: { id: string } }) => {
const ts = new Date().getTime()
return `${ts}::${id}`
return `${ts}::${arg.user.id}`
},
{
key: ({ id }: { id: string }) => id
key: arg => arg.user.id
}
)
const resultA = func({ id: 'alpha' })
const resultB = func({ id: 'beta' })
const resultA2 = func({ id: 'alpha' })
const resultA = func({ user: { id: 'alpha' } })
const resultB = func({ user: { id: 'beta' } })
const resultA2 = func({ user: { id: 'alpha' } })
assert.equal(resultA, resultA2)
assert.notEqual(resultB, resultA)
})
Expand Down

0 comments on commit f9bc525

Please sign in to comment.