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 are not detected in a deno project #145

Closed
uriva opened this issue Sep 4, 2024 · 14 comments
Closed

types are not detected in a deno project #145

uriva opened this issue Sep 4, 2024 · 14 comments
Labels
question Further information is requested

Comments

@uriva
Copy link

uriva commented Sep 4, 2024

import * as greenApi from "npm:@green-api/[email protected]";
error TS7016: Could not find a declaration file for module '@green-api/whatsapp-api-client'. '/home/runner/work/abstract-bot-api/abstract-bot-api/dist/node_modules/@green-api/whatsapp-api-client/lib/bundle.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/green-api__whatsapp-api-client` if it exists or add a new declaration (.d.ts) file containing `declare module '@green-api/whatsapp-api-client';`
@astashenkoj
Copy link

Hello! We have forwarded the question to the developers.

@kocherovv
Copy link
Contributor

Hello!
There are several ways to import the library in a project, please use one of them:
https://github.com/green-api/whatsapp-api-client-js/blob/master/README.md

image

@kocherovv kocherovv assigned kocherovv and unassigned kocherovv Sep 10, 2024
@kocherovv kocherovv added the question Further information is requested label Sep 10, 2024
@uriva
Copy link
Author

uriva commented Sep 11, 2024

@kocherovv still not working for me. Here's how to reproduce:

  1. open a directory
  2. cd into it, yarn init
  3. yarn add @green-api/whatsapp-api-client
  4. touch index.ts
  5. add import greenApi from "@green-api/whatsapp-api-client";
  6. hover over it and get a message saying type information is unavailable

@uriva
Copy link
Author

uriva commented Sep 11, 2024

the exact message:

Could not find a declaration file for module '@green-api/whatsapp-api-client'. '/home/uri/uriva/bla/node_modules/@green-api/whatsapp-api-client/lib/bundle.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/green-api__whatsapp-api-client` if it exists or add a new declaration (.d.ts) file containing `declare module '@green-api/whatsapp-api-client';`ts(7016)

@kocherovv kocherovv reopened this Sep 13, 2024
@kocherovv
Copy link
Contributor

Checking

@astashenkoj
Copy link

Hello! Thank you for waiting!
Try creating a global.d.ts file (you can choose any name) with the following content:
declare module '@green-api/whatsapp-api-client';

You also need to add the following to tsconfig:
"compilerOptions": { ... "typeRoots": ["./node_modules/@types", "./global.d.ts"] }

After which, the compiler works, there should be no errors.

@uriva
Copy link
Author

uriva commented Sep 21, 2024

The problem is that as I'm using Deno, I don't have a tsconfig nor does it do any good to have a global.d.ts file.

@uriva
Copy link
Author

uriva commented Sep 21, 2024

I mean there is this: denoland/deno#2746

But why doesn't it work like other libraries ootb? Shouldn't that be the solution?

@prostraction
Copy link

prostraction commented Sep 23, 2024

Hello, @uriva!
I don't have an experience with Deno, but I successfully imported this library using it.
As I can see, tsconfig.json or global.d.ts is not required using Deno, but it is required using tsc.
Here is what i did:

Install all necessary tools (npm is already installed):

curl -fsSL https://deno.land/install.sh | sh
export DENO_INSTALL="/root/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
npm install -g npm
npm install --global yarn

Init project:

yarn init
yarn add @green-api/whatsapp-api-client

Add code:

touch index.ts

index.ts file:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";
Deno.serve((_req: Request) => {
    const restAPI = whatsAppClient.restAPI({
        idInstance: "ID",
        apiTokenInstance: "APIToken",
    });
    restAPI.message.sendMessage("[email protected]", null, "hello world").then((data) => {
        console.log(data);
    });
    return new Response("Hello, world");
});

And now I have "Hello world" example with imported library working:
Shell 1:

# deno run --allow-net index.ts
Listening on http://0.0.0.0:8000/
✅ Granted all env access.
✅ Granted all read access.
{ idMessage: "BAE52B1C00F05BC6" }

Shell 2:

# curl localhost:8000 && echo ""
Hello, world

I used this import:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";

not

import greenApi from "npm:@green-api/whatsapp-api-client"

because of example code in Readme.

@AknurKh AknurKh closed this as completed Sep 26, 2024
@uriva
Copy link
Author

uriva commented Sep 27, 2024

@AknurKh I know it's possible to use this in deno, the problem I have is:

  1. vscode doesn't recognize the types, so I don't have code assistance
  2. look at this: https://github.com/uriva/abstract-bot-api/blob/1687ffd8e85a84cbabe8165e350ecaf89d3e0a50/src/greenApi.ts#L54 I have to reverse engineers the types myself because I can't import them

@prostraction
Copy link

@uriva Unfortunately, we can't help you with this issue, because it doesn't depend on us. This library correctly imports to Deno project and there are no errors during the execution of it. Please look up to the libraries import process of your VS Code.

@uriva
Copy link
Author

uriva commented Sep 27, 2024

@prostraction you are not answering the second question - is it possible to import the API types to use in my code?

e.g. import { GreenApiMessage } from "npm:@green-api/[email protected]"; doesn't seem to work

(this has nothing to do with vscode)

@prostraction
Copy link

@uriva it's not possible to import the API types using this library.

Code like this:

import { messageApi } from "npm:@green-api/[email protected]";

causes:

error: Uncaught SyntaxError: The requested module 'npm:@green-api/[email protected]' does not provide an export named 'messageApi'

The default import, which you can find at readme, should work:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";

@uriva
Copy link
Author

uriva commented Sep 27, 2024

Okay, so I propose a feature request to expose the API types.

They are essential for writing typescript code that uses this library, like in the example I shared above.

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

No branches or pull requests

5 participants