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

@types/gun incompatible with React CRA / Babel #37344

Closed
2 of 4 tasks
rm-rf-etc opened this issue Aug 4, 2019 · 17 comments
Closed
2 of 4 tasks

@types/gun incompatible with React CRA / Babel #37344

rm-rf-etc opened this issue Aug 4, 2019 · 17 comments

Comments

@rm-rf-etc
Copy link

rm-rf-etc commented Aug 4, 2019

I'm forced to find a workaround to use @types/gun in a create-react-app project because babel doesn't support namespaces, and likely never will. The types for GunDB are all namespaced, so this library won't work for any CRA / Babel users. Since namespaces are now considered legacy and it seems that the push is to use ES6 modules everywhere, I think it makes sense to remove namespaces from @types/gun.

To add some extra context, modifying CRA without ejecting requires some jumping thru hoops:
https://github.com/timarney/react-app-rewired/
https://github.com/arackaf/customize-cra/

FWIW, I'm trying to author a react-gun library.

Would very much appreciate some workaround suggestions.

@rm-rf-etc rm-rf-etc changed the title @types/gun incompatible with React CRA @types/gun incompatible with React CRA / Babel Aug 4, 2019
@Jack-Works
Copy link
Contributor

Yes although ESModule is good to use, sadly the gun itself is providing a complex API. Without namespace, I can't type gun in the correct way

@rm-rf-etc
Copy link
Author

Actually, it seems namespace support was merged into babel. Maybe I just need a bleeding edge version.

@rm-rf-etc
Copy link
Author

rm-rf-etc commented Aug 5, 2019

@Jack-Works, Looks like namespace support is in the latest version of @babel/plugin-transform-typescript, but I'm still not able to get the error, Cannot find namespace 'Gun'. Could you provide an example of how to import the Gun namespace, and/or something like ChainReference? import Gun from 'gun' seems to reference Gun.Constructor, and that's all I can access.

@Jack-Works
Copy link
Contributor

I'm sorry when writing typing for gun, I didn't consider it clearly. There is no way to import ChainReference and type is very unfriendly with untyped code.

When I'm free, I'll try to resolve this problem.

@rm-rf-etc
Copy link
Author

@Jack-Works okay, thank you for you help with this. I think I found a good temporary solution:

import Gun from 'gun';
const gun = Gun();
type ChainReference = typeof gun;

This seems to be working, but now I'm seeing other problems I don't understand.

If I try to use .put(), I can't pass in my data because the type for the first argument is never:

(method) Gun.ChainReference<any, any, "pre_root">.put(data: never, ...

Is this just a mistake? I see the same issue with .get():

(method) Gun.ChainReference<any, any, "pre_root">.get<string | number | symbol>(key: never, ...

@rm-rf-etc
Copy link
Author

@Jack-Works have a look at this code, this compiles and runs as expected.
https://github.com/rm-rf-etc/react-gun-test/blob/master/src/App.tsx

I've cast as any on the .once(), .get(), and .put() methods, to verify that these methods are being used how GUN expects.

I can start submitting PR's as I find these issues.

@rm-rf-etc
Copy link
Author

diff --git a/types/gun/index.d.ts b/types/gun/index.d.ts
index 0cba9d6620..1d1813fcbe 100644
--- a/types/gun/index.d.ts
+++ b/types/gun/index.d.ts
@@ -86,7 +86,7 @@ declare namespace Gun {
          * @param callback invoked on each acknowledgment
          */
         put(
-            data: Partial<AlwaysDisallowedType<DisallowPrimitives<IsTop, DisallowArray<DataType>>>>,
+            data: string | number | boolean | { [p: string]: string | number | boolean },
             callback?: AckCallback
         ): ChainReference<DataType, ReferenceKey, IsTop>;
         /**
@@ -102,7 +102,7 @@ declare namespace Gun {
          * **Here the type of callback respect to the actual behavior**
          */
         get<K extends keyof DataType>(
-            key: ArrayOf<DataType> extends never ? K : never,
+            key: string | ChainReference,
             callback?: (
                 /** the raw data. Internal node of gun. Will not typed here. */
                 paramA: Record<
@@ -156,7 +156,7 @@ declare namespace Gun {
          */
         once(
             callback?: (
-                data: (DisallowPrimitives<IsTop, AlwaysDisallowedType<ArrayAsRecord<DataType>>>) | undefined,
+                data: string | number | boolean | (DisallowPrimitives<IsTop, AlwaysDisallowedType<ArrayAsRecord<DataType>>>) | undefined,
                 key: ReferenceKey
             ) => void,
             option?: { wait: number }

@Jack-Works, here's some changes I made in the type definitions. I cloned this to my local and used npm link to use these definitions in my project. I was able to remove the cast as any and compile:
https://github.com/rm-rf-etc/react-gun-test/blob/npm-linked-types-gun/src/App.tsx

@Jack-Works
Copy link
Contributor

@Jack-Works okay, thank you for you help with this. I think I found a good temporary solution:

import Gun from 'gun';
const gun = Gun();
type ChainReference = typeof gun;

This seems to be working, but now I'm seeing other problems I don't understand.

If I try to use .put(), I can't pass in my data because the type for the first argument is never:

(method) Gun.ChainReference<any, any, "pre_root">.put(data: never, ...

Is this just a mistake? I see the same issue with .get():

(method) Gun.ChainReference<any, any, "pre_root">.get<string | number | symbol>(key: never, ...

It's by design. With typed interface, methods like get and put will act normal.

@rm-rf-etc
Copy link
Author

@Jack-Works okay, thank you for you help with this. I think I found a good temporary solution:
It's by design. With typed interface, methods like get and put will act normal.

Could you elaborate? Maybe include an example? Shouldn't get and put accept – be default – a type which includes all valid gun data types?

@Jack-Works
Copy link
Contributor

@Jack-Works okay, thank you for you help with this. I think I found a good temporary solution:
It's by design. With typed interface, methods like get and put will act normal.

Could you elaborate? Maybe include an example? Shouldn't get and put accept – be default – a type which includes all valid gun data types?

You can see the test example in the @types/gun

@rm-rf-etc
Copy link
Author

I understand now. In the next version, will ChainReference be exported? It might help me write the types for react-gun.

@Jack-Works
Copy link
Contributor

Jack-Works commented Aug 7, 2019 via email

@Jack-Works
Copy link
Contributor

Got no idea how to do this well, maybe I need to contribute to gun and let them to use esmodule first

@Jack-Works
Copy link
Contributor

amark/gun#789

@Jack-Works
Copy link
Contributor

Jack-Works commented Aug 15, 2019 via email

@Jack-Works
Copy link
Contributor

I have seen the experimental es module export gun, I'll rewrite the types later, make it more friendly to TypeScript and non-typescript users.

@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

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