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.35k stars 353 forks source link

LRUCache is both a namespace and a class! #327

Closed JimmyBjorklund closed 9 months ago

JimmyBjorklund commented 9 months ago

LRUCache is both a namespace and a class! This is a really bad chois it causes build issues for user of the library in later stages. Strongly recommend changing one of them to a new name. /J

isaacs commented 9 months ago

What build issues does it cause? Please provide reproduction, showing expectation and actual behavior.

JimmyBjorklund commented 9 months ago

We are using "coap": "^1.3.0", and when building it seams to get confused about the namespace and class. It should work just fine but for some reason it takes the namespace instead of the class. Thay do this in there code https://github.com/coapjs/node-coap/blob/master/lib/server.ts

class CoapLRUCache<K extends {}, V extends {}> extends LRUCache<K, V> { pruneTimer: NodeJS.Timeout }

node_modules/coap/dist/lib/server.d.ts:11:10 - error TS2617: 'LRUCache' can only be imported by using 'import LRUCache = require("lru-cache")' or by turning on the 'esModuleInterop' flag and using a default import.

11 import { LRUCache } from 'lru-cache';

isaacs commented 9 months ago

Give me some code and a command to run so that I can see it happening. LRUCache seems to build just fine in every situation I've used it, so there's something particular to either your setup or theirs, but if I can't reproduce it, I cannot help you, sorry.

JimmyBjorklund commented 9 months ago

I created a smal sample, the first error is easy to get around and thats just to add { } aroung 3 import BufferListStream from 'bl'; But the last one seams to get the compiler to go bananas, npm run start works but npm run build do not. Only thing i can think of is that its to do with the namespace and class name. https://github.com/JimmyBjorklund/t-coap

isaacs commented 9 months ago

That is not the problem.

node_modules/coap/dist/lib/server.d.ts:11:8 - error TS1259: Module '"/Users/isaacs/dev/isaacs/lru-cache/t-coap/node_modules/@types/lru-cache/index"' can only be default-imported using the 'esModuleInterop' flag

11 import LRUCache from 'lru-cache';
          ~~~~~~~~

This patch will fix the issue for you:

diff --git a/tsconfig.json b/tsconfig.json
index b1937c9..6229b40 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,6 +1,7 @@
 {
     "compileOnSave": true,
     "compilerOptions": {
+      "esModuleInterop": true,
       "allowJs": true,
       "baseUrl": ".",
       "experimentalDecorators": true,

Or, since really this is an issue with the coap lib, not your code, you could use this patch to avoid type checking code you don't own:

diff --git a/tsconfig.json b/tsconfig.json
index b1937c9..5dcef04 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -2,6 +2,7 @@
     "compileOnSave": true,
     "compilerOptions": {
       "allowJs": true,
+      "skipLibCheck": true,
       "baseUrl": ".",
       "experimentalDecorators": true,
       "emitDecoratorMetadata": true,

Not a problem with lru-cache.

isaacs commented 9 months ago

Also, coap is using lru-cache v6 (current is v10.2.0). If they upgrade to the latest lru-cache, then they can also ditch @types/lru-cache, because the types are included.

I recommend you suggest this to them.

JimmyBjorklund commented 9 months ago

Thanks.