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

Cast QuickLRU to Map #34

Closed
szmarczak opened this issue Aug 16, 2021 · 2 comments · Fixed by #37
Closed

Cast QuickLRU to Map #34

szmarczak opened this issue Aug 16, 2021 · 2 comments · Fixed by #37

Comments

@szmarczak
Copy link

const lru = new QuickLRU<string, string>({maxSize: 1000});
const map = lru as Map<string, string>;

Currently it throws this:

Argument of type 'QuickLRU<string, string>' is not assignable to parameter of type 'Map<string, string>'.
  Type 'QuickLRU<string, string>' is missing the following properties from type 'Map<string, string>':
    forEach, entries, [Symbol.toStringTag]

ts(2345)
@sindresorhus
Copy link
Owner

I guess we could implement those methods just to have Map compatibility and discourage their use for anything other than when something expects a Map.

@gthau
Copy link

gthau commented Sep 15, 2021

Maybe you could instead export the cache as a new Map object. In doing so, you preserve the cache and the exported map behaves as expected.
If you just cast the cache to Map, then your resulting map is of type Map<string, string | undefined> not Map<string, string> because a value might be stalled and therefore map.get(key) would return undefined, not the expected value.
If you provide a method .toMap(), you can already filter the stalled values from the Map.

javi11 pushed a commit to javi11/quick-lru-cjs that referenced this issue Nov 19, 2021
maxAge of .set method was expecting the expiry value.

fix: .set must expect the maxAge in milliseconds

Closes sindresorhus#34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants