planttheidea/micro-memoize

Function signatures are lost when memoized in version 3

Closed this issue · 3 comments

Hi there! It seems that in version 3, memoized functions do not keep their type signatures. Can we fix support for this? I checked the type definitions and this can probably be done with:

export default function memoize<T extends Function>(
  fn: T | MicroMemoize.Memoized,
  options?: MicroMemoize.Options,
): T

Or:

export default function memoize<T extends Function>(
  fn: T | MicroMemoize.Memoized,
  options?: MicroMemoize.Options,
): T & MicroMemoize.Memoized;

Happy to, but can you provide an example of code that produces the issue? That way a test can be written to ensure the change works.

Yeah of course, this should not be possible:

function add(a: number, b: number): number {
  return a + b;
}

const addMemoized = memoize(add);

addMemoized('bla'); // TypeScript should complain here with "Expected 2 arguments, but got 1." but it currently does not complain.

I just released 3.0.2, which should fix the signatures not passing through. It also gave me a chance to holistically review my types, since my TypeScript-fu has improved since I wrote this. It should be better now, but if you have anything else, don't hesitate to ask!