sindresorhus / p-memoize

Memoize promise-returning & async functions
MIT License
396 stars 30 forks source link

How to get it working TypeScript? #14

Closed rfgamaral closed 3 years ago

rfgamaral commented 4 years ago

I've followed the example in the README.md but I get this:

image

However, replacing p-memoize with mem works just fine in TypeScript without any issues.

This leads me to two questions:

  1. What am I missing to get p-memoize properly working with TypeScript?
  2. What exactly is the advantage of p-memoize over mem for using with got?
klvenky commented 4 years ago

I think it's a little too late to answer your question, but the issue could be with the memGot not having a type defined. Instead it can be often improved as below.

const memGot = (url:string, opts: GotOptions<string>) => pMemoize(() => got(url, opts), {maxAge: 6000});

It would be better to follow this syntax as the options passed to the got function can be varying and this will help in keeping the code clean.

Also the advantage of using p-memoize over mem is explained here. Answered best of my knowledge. Correct me if I'm wrong.

fregante commented 4 years ago

What am I missing to get p-memoize properly working with TypeScript?

Nothing, probably the types are wrong.

What exactly is the advantage of p-memoize over mem for using with got?

No advantage unless you're using cachePromiseRejection. p-memoize is a duplicate module and just wraps mem to add that option.

Also the advantage of using p-memoize over mem is explained here.

Nothing there mentions p-memoize; all of that also applies to p-memoize

kentcdodds commented 3 years ago

For anyone else having TS issues with p-memoize, after reading this: "No advantage unless you're using cachePromiseRejection" and realizing that I'm not using that feature, I switched to mem and TS issues disappeared.