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

LRUCache seems to incorrectly implement Map<K, V>

Open vacavaca opened this issue 1 year ago • 1 comments

typescript: 5.4.3 node: 21.7.1 lru-cache: 10.2.0 @types/node: 20.11.30

Got the following error from inside the lru-cache when running tsc on my project, could it be something on my side? Earlier versions, before implementing Map, are not affected.

> tsc

node_modules/lru-cache/dist/commonjs/index.d.ts:762:5 - error TS2416: Property 'forEach' in type 'LRUCache<K, V, FC>' is not assignable to the same property in base type 'Map<K, V>'.
  Type '(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => any, thisp?: any) => void' is not assignable to type '(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) => void'.
    Types of parameters 'fn' and 'callbackfn' are incompatible.
      Types of parameters 'map' and 'self' are incompatible.
        Property 'toJSON' is missing in type 'LRUCache<K, V, FC>' but required in type 'Map<K, V>'.

762     forEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => any, thisp?: any): void;
        ~~~~~~~

  ../../node_modules/@types/core-js/index.d.ts:68:5
    68     toJSON(): any;
           ~~~~~~~~~~~~~~
    'toJSON' is declared here.

vacavaca avatar Mar 26 '24 10:03 vacavaca

So it needs a toJSON method?

isaacs avatar Mar 26 '24 16:03 isaacs

Yeah, idk what @types/core-js thinks Map.toJSON is, but JavaScript doesn't define that.

> m = new Map([[1,2],[3,4]])
Map(2) { 1 => 2, 3 => 4 }
> JSON.stringify(m)
'{}'
> m.toJSON()
Uncaught TypeError: m.toJSON is not a function

Closing until someone can tell me what the actual problem is. I think this is an issue with @types/core-js, though, not lru-cache

isaacs avatar Jun 27 '24 22:06 isaacs