sindresorhus / p-memoize

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

Type of ReturnType passed to mem options is incorrect #28

Closed henhal closed 3 years ago

henhal commented 3 years ago

The current interface in index.d.ts:

    <ArgumentsType extends any[], ReturnType, CacheKeyType>(
        fn: (...arguments: ArgumentsType) => PromiseLike<ReturnType>,
        options?: pMemoize.Options<ArgumentsType, CacheKeyType, PromiseLike<ReturnType>>
    ): (...arguments: ArgumentsType) => Promise<ReturnType>;

This means that if you pass a fn returning Promise<MyType>, and you need to use your own custom cache, you need to pass e.g. Map<string, {data: MyType, maxAge: number}>, which is incorrect; the cache will store Promise<MyType> in the data field of each cache entry.

Currently the only way to compile code using p-memoize and a custom cache is to type the custom cache values as any.

By using the same type for the return type of fn and the data entries, the error is resolved.

Fixed by https://github.com/sindresorhus/p-memoize/pull/27

sindresorhus commented 3 years ago

Fixed by #27