isaacs / node-lru-cache

A fast cache that automatically deletes the least recently used items
http://isaacs.github.io/node-lru-cache/
ISC License
5.38k stars 353 forks source link

Typescript error: Property 'forEach' in type 'LRUCache<K, V, FC>' is not assignable to the same property in base type 'Map<K, V>'. #354

Closed paulsmithkc closed 2 months ago

paulsmithkc commented 2 months ago

The following typescript error currently exists in lru-cache@10.4.3 which is causing build errors for users of:

  1. lru-cache@10.4.3
  2. path-scurry@1.11.1
  3. glob@10.4.5
  4. rimraf@5.0.10
node_modules/.pnpm/lru-cache@10.4.3/node_modules/lru-cache/dist/commonjs/index.d.ts:1032: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.
        Type 'LRUCache<K, V, FC>' is not assignable to type 'Map<K, V>'.
          The types returned by 'entries().next(...)' are incompatible between these types.
            Type 'IteratorResult<[K, V], void>' is not assignable to type 'IteratorResult<[K, V], undefined>'.
              Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorResult<[K, V], undefined>'.
                Type 'IteratorReturnResult<void>' is not assignable to type 'IteratorReturnResult<undefined>'.
                  Type 'void' is not assignable to type 'undefined'.

1032     forEach(fn: (v: V, k: K, self: LRUCache<K, V, FC>) => any, thisp?: any): void;
paulsmithkc commented 2 months ago

Upgrading to rimraf@6.0.1 seems to resolve this issue.

isaacs commented 2 months ago

Upgrade to latest, and set skipLibCheck: true in your tsconfig.

marekdedic commented 2 months ago

Hi, I am on rimraf 6.0.1 and I still get this issue.

skipLibCheck is an escape hatch to make things work with incorrect and/or incompatible typings. I'd prefer to fix the typings here instead. Happy to send a PR.

LoicPoullain commented 2 months ago

This has been fixed for me by downgrading TypeScript v5.6.2 to v5.5.4.

marekdedic commented 2 months ago

@LoicPoullain I have also noted several new errors with TS 5.6 - do you have any idea if that's a regression or intentional?

LoicPoullain commented 2 months ago

@marekdedic no idea

isaacs commented 2 months ago

This was fixed on https://github.com/isaacs/node-lru-cache/commit/5ed947ddc8a26b5f2727f9c66f0ef835a9ede0db released in lru-cache v11.0.1.

If you are in that version, you don't have this bug. Whatever problem you have is a different problem. Please provide details in a new issue.

If you are not on that version, upgrade. I do not need a pull request, because it is already fixed and shipped.

skipLibCheck: true is recommended by the typescript team officially. It is an "escape hatch" for a mistake in the design of the language.

This issue is resolved, and should not be an issue reported here in the first place. Please stop.