Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch: truthy promise resolution typing #329

Open
mfulton26 opened this issue Feb 8, 2024 · 0 comments
Open

fetch: truthy promise resolution typing #329

mfulton26 opened this issue Feb 8, 2024 · 0 comments

Comments

@mfulton26
Copy link

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant