erikras / lru-memoize

A utility to provide LRU memoization for any js function
MIT License
316 stars 20 forks source link

Typescript error #92

Open aarongreenwald opened 5 years ago

aarongreenwald commented 5 years ago

Starting today, apparently with the release of v1.1.0, we started getting the following error when running TS compilation on our project that uses lru-memoize:

node_modules/lru-memoize/dist/index.d.ts:1:25 - error TS1254: A 'const' initializer 
in an ambient context must be a string or numeric literal or literal enum reference.

  1 declare const memoize = (
                            ~
  2   limit?: number,
    ~~~~~~~~~~~~~~~~~
...
  4   deepObjects?: boolean
    ~~~~~~~~~~~~~~~~~~~~~~~
  5 ) => <T extends Function>(func: T) => T;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/lru-memoize/dist/index.d.ts:5:39 - error TS2693: 'T' only refers to a type, 
but is being used as a value here.

5 ) => <T extends Function>(func: T) => T;
                                        ~

Found 2 errors.

Our tsconfig.json looks like this:

{
  "compilerOptions": {
    "declaration": true,
    "allowJs": false,
    "target": "ESNEXT",
    "module": "commonjs",
    "noImplicitAny": false,
    "noImplicitThis": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "lib": ["es2015", "es2017.object"],
    "outDir": "./lib"
  },
  "exclude": [
    "node_modules",
    "dist",
    "test"
  ]
}

Any ideas what we're doing wrong? When I downgrade to version 1.0.2 of lru-memoize everything works.

KhodeN commented 5 years ago

May be the right way is

declare const memoize : (
  limit?: number,
  ...

or even

export default function memoize(
  limit?: number,
  equals?: (a: any, b: any) => boolean,
  deepObjects?: boolean
) => <T extends Function>(func: T) => T;
johnryan commented 5 years ago

Any update on this?

airhorns commented 5 years ago

@KhodeN is right on, this type definition is working fine for me:

declare module "lru-memoize" {
  function Memoizer(
    limit?: number,
    equals?: (a: any, b: any) => boolean,
    deepObjects?: boolean
  ): <T extends (...args: any[]) => any>(func: T) => (...funcArgs: Parameters<T>) => ReturnType<T>;
  export default Memoizer;
}