Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type errors #37

Closed
sukazavr opened this issue Jun 11, 2019 · 7 comments · Fixed by #38
Closed

Type errors #37

sukazavr opened this issue Jun 11, 2019 · 7 comments · Fixed by #38

Comments

@sukazavr
Copy link

Hello!

"micro-memoize": "^4.0.6"

I have a bunch of type errors related with micro-memoize:

ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(33,7):
TS2322: Type '(keyToMatch: any[]) => number' is not assignable to type 'KeyIndexGetter'.
  Types of parameters 'keyToMatch' and 'keyToMatch' are incompatible.
    Type 'RawKey' is not assignable to type 'any[]'.
      Type 'IArguments' is missing the following properties from type 'any[]': pop, push, concat, join, and 24 more.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(34,16):
TS2532: Object is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(35,7):
TS2322: Type '(keyToMatch: any[]) => number' is not assignable to type 'KeyIndexGetter'.
  Types of parameters 'keyToMatch' and 'keyToMatch' are incompatible.
    Type 'RawKey' is not assignable to type 'any[]'.
      Type 'IArguments' is not assignable to type 'any[]'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(37,7):
TS2322: Type '(keyToMatch: any[]) => 0 | -1' is not assignable to type 'KeyIndexGetter'.
  Types of parameters 'keyToMatch' and 'keyToMatch' are incompatible.
    Type 'RawKey' is not assignable to type 'any[]'.
      Type 'IArguments' is not assignable to type 'any[]'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(79,9):
TS2532: Object is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(79,9):
TS2722: Cannot invoke an object which is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(83,9):
TS2532: Object is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(85,13):
TS2722: Cannot invoke an object which is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(125,16):
TS2722: Cannot invoke an object which is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(165,12):
TS2722: Cannot invoke an object which is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(204,33):
TS2532: Object is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(206,7):
TS2322: Type 'number | undefined' is not assignable to type 'number'.
  Type 'undefined' is not assignable to type 'number'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(206,21):
TS2322: Type 'number | undefined' is not assignable to type 'number'.
  Type 'undefined' is not assignable to type 'number'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(228,11):
TS2722: Cannot invoke an object which is possibly 'undefined'.
ERROR in D:/Tripment/code/http-services-with-general-components/node_modules/micro-memoize/src/Cache.ts(232,11):
TS2722: Cannot invoke an object which is possibly 'undefined'.
Version: typescript 3.4.5, tslint 5.16.0

tsconfig

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["dom", "es2017"],
    "moduleResolution": "node",
    "esModuleInterop": true,
    "strict": true,

    /* Additional Checks */
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,

    /* Debugging Options */
    "traceResolution": false /* Report module resolution log messages. */,
    "listEmittedFiles": false /* Print names of generated files part of the compilation. */,
    "listFiles": false /* Print names of files part of the compilation. */,
    "pretty": true /* Stylize errors and messages using color and context. */,

    /* Experimental Options */
    // "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
    // "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,

    "jsx": "preserve"
  },
  "compileOnSave": false,
  "exclude": ["node_modules/**/*"]
}
@planttheidea
Copy link
Owner

Hi! Happy to investigate this, but can you give me a code example of what produces this error?

@sukazavr
Copy link
Author

Sure thing!

import memoize from 'micro-memoize'

function callAPI<T>(url: string): Promise<T> {
	return fetch(url).then((res) => res.json())
}

const getCountries = memoize(
	async () => {
		const { Countries } = await callAPI<{
			Countries: ICountryAPI[]
		}>('https://fdgdfgdfgdfgdfg.com/api/countries')
		return Countries
	},
	{ isPromise: true }
)

I think the problem is that I use strict mode in my TS-config, but your package doesn't, so that's why your package types incompatible with my code.

planttheidea added a commit that referenced this issue Jun 12, 2019
@planttheidea
Copy link
Owner

Alright I have a PR up, and it isn't great but it should do the trick. Inherently when building these kinds of libraries where you use raw JS hacks for performance, the "clean code" methodology TS espouses kind of gets compromised.

I have published a beta of the patch fix, you should be able to install as micro-memoize@next. If you could try that and let me know the results I'd appreciate it.

@sukazavr
Copy link
Author

Yes! It works!
Thank you.
Great library and author :-)

planttheidea added a commit that referenced this issue Jun 14, 2019
* use strict: true in tsconfig

attempt to fix #37

* update exposed types

also, change PassedOptions to Options to be backwards compatible

* Release 4.0.8-beta.0
@ondrejvelisek
Copy link

Hi there,

I have similar problem. TS 4.1 introduced new compilerOption noUncheckedIndexedAccess. see:
https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess

When I use TS 4.1 or above with this option, micro-memoize fails to compile.

To reproduce bug, create a project with TS 4.1 or above, turn on noUncheckedIndexedAccess, import and use micro-memoize and run a TS build. I see:

tsc --noEmit

node_modules/micro-memoize/src/Cache.ts:82:23 - error TS2345: Argument of type 'Key | undefined' is not assignable to parameter of type 'Key'.
  Type 'undefined' is not assignable to type 'any[]'.

82     if (isMatchingKey(keys[0], keyToMatch)) {
                         ~~~~~~~

node_modules/micro-memoize/src/Cache.ts:88:27 - error TS2345: Argument of type 'Key | undefined' is not assignable to parameter of type 'Key'.
  Type 'undefined' is not assignable to type 'any[]'.

88         if (isMatchingKey(keys[index], keyToMatch)) {
                             ~~~~~~~~~~~

node_modules/micro-memoize/src/Cache.ts:176:13 - error TS2339: Property 'length' does not exist on type 'Key | undefined'.

176     const { length } = existingKey;
                ~~~~~~

node_modules/micro-memoize/src/Cache.ts:186:22 - error TS2532: Object is possibly 'undefined'.

186         if (!isEqual(existingKey[index], keyToMatch[index])) {
                         ~~~~~~~~~~~

node_modules/micro-memoize/src/Cache.ts:194:20 - error TS2532: Object is possibly 'undefined'.

194     return isEqual(existingKey[0], keyToMatch[0]) ? 0 : -1;
                       ~~~~~~~~~~~

node_modules/micro-memoize/src/Cache.ts:220:7 - error TS2322: Type 'Key | undefined' is not assignable to type 'Key'.
  Type 'undefined' is not assignable to type 'any[]'.

220       keys[index + 1] = keys[index];
          ~~~~~~~~~~~~~~~

node_modules/micro-memoize/src/Cache.ts:269:43 - error TS2345: Argument of type 'Key | undefined' is not assignable to parameter of type 'RawKey'.
  Type 'undefined' is not assignable to type 'RawKey'.

269         const keyIndex = this.getKeyIndex(firstKey);
                                              ~~~~~~~~


Found 7 errors in the same file, starting at: node_modules/micro-memoize/src/Cache.ts:82

@planttheidea
Copy link
Owner

planttheidea commented Apr 21, 2022

Alright I can tackle this over the weekend I think, but @ondrejvelisek would you mind creating a new issue for this? It will help with mapping the solution to the issue.

@ondrejvelisek
Copy link

Sure thing. It is here: #76

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants