sindresorhus / memoize

Memoize functions - an optimization technique used to speed up consecutive function calls by caching the result of calls with identical input

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

5.1.0 cleanup broke cache typing

dylang opened this issue · comments

in index.d.ts the cache is made up of data and maxAge,

readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number}>;

However, in index.js, the cache could be just the data (ReturnType).

		cache.set(key, maxAge ? {
			data: cacheItem,
			maxAge: Date.now() + maxAge
		} : cacheItem);

This will probably only effect those using TypeScript and providing a custom cache.

I think the fix is a simple change the type to

readonly cache?: CacheStorage<CacheKeyType, {data: ReturnType; maxAge: number} | ReturnType>;

I tried this locally and it seemed to work for my case, which does not make use of maxAge.

@bfred-it Maybe we should just always set an object, but make maxAge: Infinity if the maxAge option is not specified?

This can be typed correctly by someone who knows TS enough, but I guess it's not a huge deal to undo that change.