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

focusAtom type error #6

Closed
ViktorQvarfordt opened this issue Feb 1, 2023 · 14 comments
Closed

focusAtom type error #6

ViktorQvarfordt opened this issue Feb 1, 2023 · 14 comments

Comments

@ViktorQvarfordt
Copy link

ViktorQvarfordt commented Feb 1, 2023

The example code from the docs

const baseAtom = atom({ a: 5 }) // PrimitiveAtom<{a: number}>
const derivedAtom = focusAtom(baseAtom, (optic) => optic.prop('a')) // PrimitiveAtom<number>

gives this typescript error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'PrimitiveAtom<{ a: number; }> & WithInitialValue<{ a: number; }>' is not assignable to parameter of type 'WritableAtom<{ a: number; } | undefined, [{ a: number; }], void>' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
      Types of property 'onMount' are incompatible.
        Type 'OnMount<SetStateAction<{ a: number; }>, void>' is not assignable to type 'OnMount<[{ a: number; }], void>'.
          Types of parameters 'setAtom' and 'setAtom' are incompatible.
            Type 'S' is not assignable to type '(update: SetStateAction<{ a: number; }>) => void'.
              Type 'SetAtom<[{ a: number; }], void>' is not assignable to type '(update: SetStateAction<{ a: number; }>) => void'.
                Types of parameters 'args_0' and 'update' are incompatible.
                  Type 'SetStateAction<{ a: number; }>' is not assignable to type '{ a: number; }'.
                    Type '(prev: { a: number; }) => { a: number; }' is not assignable to type '{ a: number; }'.ts(2769)
focusAtom.d.ts(15, 25): The last overload is declared here.

Getting a similar error message also after disabling exactOptionalPropertyTypes as the error message suggests:

No overload matches this call.
  The last overload gave the following error.
    Argument of type 'PrimitiveAtom<{ a: number; }> & WithInitialValue<{ a: number; }>' is not assignable to parameter of type 'WritableAtom<{ a: number; } | undefined, [{ a: number; }], void>'.
      Types of property 'onMount' are incompatible.
        Type 'OnMount<SetStateAction<{ a: number; }>, void> | undefined' is not assignable to type 'OnMount<[{ a: number; }], void> | undefined'.
          Type 'OnMount<SetStateAction<{ a: number; }>, void>' is not assignable to type 'OnMount<[{ a: number; }], void>'.
            Types of parameters 'setAtom' and 'setAtom' are incompatible.
              Type 'S' is not assignable to type '(update: SetStateAction<{ a: number; }>) => void'.
                Type 'SetAtom<[{ a: number; }], void>' is not assignable to type '(update: SetStateAction<{ a: number; }>) => void'.
                  Types of parameters 'args_0' and 'update' are incompatible.
                    Type 'SetStateAction<{ a: number; }>' is not assignable to type '{ a: number; }'.
                      Type '(prev: { a: number; }) => { a: number; }' is not assignable to type '{ a: number; }'.ts(2769)
focusAtom.d.ts(15, 25): The last overload is declared here.

Screenshot for reference:
image

Using these versions

    "jotai": "^2.0.0",
    "jotai-optics": "^0.3.0",
    "optics-ts": "^2.4.0",
@ViktorQvarfordt
Copy link
Author

Localized the issue to "moduleResolution": "nodenext" in tsconfig.

Here is a type of minimal reproducible example. It doesn't show the same verbose error message as in my post above, but it does show that the type does not get inferred correctly.

@dai-shi
Copy link
Member

dai-shi commented Feb 1, 2023

Works fine here: https://tsplay.dev/WJ7L6w
image

So, it's a bundler/bundling issue??

@ViktorQvarfordt
Copy link
Author

It loks like that site doesn’t allow to change the tsconfig.

I wouldn’t say it’s a bundler issue but rather an issue that occurs under certain typescript settings.

@dai-shi
Copy link
Member

dai-shi commented Feb 2, 2023

so, how can we fix it on the library end?

@arjunvegda
Copy link
Member

I haven't looked too deeply into this, but could the root cause be optics-ts?

When module resolution is set to node and optics-ts resolves the types correctly. See - https://tsplay.dev/wgAP0m

When module resolution is set to nodenext, optics-ts doesn't resolve the types correctly. See - https://tsplay.dev/WYRzdw . It appears to be resolving to the cjs module but can't find types for it.

image

@4ortytwo
Copy link

Just had this error after updating Jotai to v2. I was on TS 4.7.4. Updated to 5.0.4. and everything is ok.

@cristatus
Copy link
Contributor

I was facing the same issue with moduleResolution: "bundler". I tried patching the optics-ts to export types declaration file from the exports section. It fixed the unknown type issue.

 "exports": {
     ".": {
       "types": "./index.d.ts",
       "import": "./dist/mjs/index.js",
       "require": "./dist/cjs/index.js"
     },

but than I saw Type instantiation is excessively deep and possibly infinite. error with focusAtom function.

This issue is fixed by patching the dist/src/focusAtom.d.ts by replacing O.OpticFor<S> with O.OpticFor_<S>.

Will try to submit PR to respective repos.

@Aleksion
Copy link

Aleksion commented Jul 7, 2023

@cristatus did you submit a PR for it? focusAtom is basically killing my vscode editor because of this 😂

@cristatus
Copy link
Contributor

Just created here #10 and akheron/optics-ts#318

@arjunvegda
Copy link
Member

@Aleksion - Would you like to test it on your end with the canary version? Here are the instructions to install @cristatus's solution

@Aleksion
Copy link

Aleksion commented Jul 9, 2023

I did - and it solved it. (I forked and tried it manually but didn't get to push it).

So all good from here 🎉

@arjunvegda
Copy link
Member

arjunvegda commented Jul 9, 2023

I just checked that this PR (from @cristatus) on optics-ts repo is merged. I think we should wait for it to be released and see if that fixes the issue 🤔

@arjunvegda
Copy link
Member

Are we good to close this issue? Did akheron/optics-ts#318 fix it?

@arjunvegda
Copy link
Member

The fix is released in v0.3.1 🚀

axeloradmin pushed a commit to axelor/axelor-open-platform that referenced this issue Sep 14, 2023
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

No branches or pull requests

6 participants