sindresorhus/quick-lru

Cast `QuickLRU` to `Map`

szmarczak opened this issue · 2 comments

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)

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 commented

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.