node-lru-cache icon indicating copy to clipboard operation
node-lru-cache copied to clipboard

fetch: truthy promise resolution typing

Open mfulton26 opened this issue 1 year ago • 0 comments

The return type for fetch in each of the current two overloads is Promise<undefined | V>.

However, fetch should always resolve to V except when certain options are set (e.g. ignoreFetchAbort and allowStaleOnFetchAbort).

As the return type is now, callers must resort to solutions like ! to tell TypeScript type checking that the value won't be undefined:

const value: V | undefined = await cache.fetch(key);
const value: V = (await cache.fetch(key))!;

Is it possible to add additional overrides for fetch to define return a return type of Promise<V> for whenever the LRUCache instance wasn't constructed with options that would allow resolving to undefined? If that is too complicated or otherwise isn't feasible at this time, then what about adding an alternative method that always fetches or throws independent of those options which never resolves to undefined (e.g. fetchOrError)?

const value: V = await cache.fetch(key);

or

const value: V = await cache.fetchOrError(key);

mfulton26 avatar Feb 08 '24 14:02 mfulton26