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

Typescript errors (multiple) #567

Closed
sshillyer opened this issue Aug 20, 2018 · 12 comments
Closed

Typescript errors (multiple) #567

sshillyer opened this issue Aug 20, 2018 · 12 comments
Assignees
Labels
Bug A code defect that needs to be fixed. Typescript This issue is about Typescript.

Comments

@sshillyer
Copy link

Trying to bootstrap a project with arangojs integration and getting errors trying to compile into typescript.

I ran in a new project 'npm install --save arangojs' then setup the below index.ts and tried running typscript compile 'tsc index.ts' and received multiple errors.

The code I'm executing is basically the sample from the tutorials.

contents of index.ts:

import { Database, aql } from 'arangojs';

export let db = new Database();

(async function () {
   const now = Date.now();
   try {
      const cursor = await db.query(aql`
      RETURN ${now}
    `);
      const result = await cursor.next();
      return result;
      // ...
   } catch (err) {
      // ...
   }
})();

errors:

> tsc index.ts
node_modules/arangojs/lib/cjs/database.d.ts:1:23 - error TS2688: Cannot find type definition file for 'node'.

1 /// <reference types="node" />
                        ~~~~


node_modules/arangojs/lib/cjs/database.d.ts:101:45 - error TS2304: Cannot find name 'Buffer'.

101     downloadService(mount: string): Promise<Buffer | Blob>;
                                                ~~~~~~


node_modules/arangojs/lib/cjs/util/request.node.d.ts:1:23 - error TS2688: Cannot find type definition file for 'node'.

1 /// <reference types="node" />
                        ~~~~


node_modules/arangojs/lib/cjs/util/request.node.d.ts:2:48 - error TS2307: Cannot find module 'http'.

2 import { ClientRequest, IncomingMessage } from "http";
                                                 ~~~~~~


node_modules/arangojs/lib/cjs/util/request.node.d.ts:3:21 - error TS2307: Cannot find module 'url'.

3 import { Url } from "url";
                      ~~~~~
@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

Try installing @types/node. That's generally good advice when using TypeScript for a node.js project.

@pluma pluma added the Question Probably not a bug. label Aug 27, 2018
@pluma pluma self-assigned this Aug 27, 2018
@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

FYI this is more of a general TypeScript usage question rather than one related to arangojs.

I'd recommend trying out Visual Studio Code (free, lightweight and cross-platform) which has awesome TypeScript integration and makes it easy to catch and auto-fix these problems before even compiling once.

@sshillyer
Copy link
Author

I installed @types/node. The above typescript errors went away, except for the type Blob.

No other package I've installed has required me to manually install various components. The type "Blob" is in the arangojs file referenced in the below error, which is the one that I meant to post in the issue. I can "fix" the problem by removing "Blob" as a type for the downloadService function signature response so that it is a Promise, but no idea where to get "blob" type imported.

I have VS Code and have used for over a year - it does not automagically fix problems like this. Thanks for the suggestion though.

node_modules/arangojs/lib/cjs/database.d.ts:101:54 - error TS2304: Cannot find name 'Blob'.

101     downloadService(mount: string): Promise<Buffer | Blob>;
                                                         ~~~~

[1:28:52 PM] Found 1 error. Watching for file changes.

What do you suggest I do about this error if @types/node install doesn't fix?

@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

Hm... odd. Can you try setting "skipLibCheck": true in your tsconfig.json?

I wonder why you're not getting similar issues with other libraries that use node built-in modules (and especially Buffer, which is a node global).

The Blob you're seeing is for browser compatibility. Try adding "dom" to your tsconfig.json file's "lib" array.

I didn't mean to imply VSCode makes the errors go away by itself, but VSCode's autofix (light bulb) feature usually provides ways to solve these problems (e.g. suggesting missing type definitions or imports).

@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

Can you tell me what version of TypeScript you're using and paste your tsconfig.json? That would make it easier to replicate your issue.

@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

FYI, I tested the following:

mkdir ts-demo
cd ts-demo
yarn init -y
yarn add --dev typescript @types/node
yarn add arangojs
yarn tsc --init
cat <<EOF > index.ts
import { Database } from "arangojs"
const db = new Database();
db.acquireHostList();
EOF
yarn tsc

and it doesn't result in any errors. I also don't see any errors doing the same with typescript@2.

EDIT: Omitting the @types/node of course I see errors on the usage of modules like url and friends, as well as Buffer.

EDIT2: Any library I can think of that would use built-in node modules (e.g. express) doesn't provide its own typings. Those libraries usually have @types/node as the dependency of their DefinitelyTyped typings. But depending on that module directly seems improper for a library.

@pluma pluma added Bug A code defect that needs to be fixed. Typescript This issue is about Typescript. and removed Question Probably not a bug. labels Aug 27, 2018
@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

Okay, looks like the best practice is hidden in this guide: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html ... and it's to add the typing as an explicit dependency.

So I guess this is a bug after all.

@pluma pluma closed this as completed in af7e213 Aug 27, 2018
@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

The fix for the missing @types/node dependency will be released with the other changes once ArangoDB 3.4RC1 is API stable (hopefully later this week).

If you can provide a way to reproduce the error concerning Blob, that would help diagnosing that issue. But since it's not reproducible with the default configuration generated by tsc --init nor the one generated by create-react-app --scripts-version=react-scripts-ts, I don't think that's a bug with arangojs.

EDIT: Ideally we'd like to be able to provide separate typings for the browser and node versions of arangojs, but the overhead of allowing TS to use the DOM lib (the default) seems negligible compared to the effort required to maintain two separate versions with separate typings.

pluma added a commit that referenced this issue Aug 27, 2018
@pluma
Copy link
Contributor

pluma commented Aug 27, 2018

@sshillyer I had to rewrite history a bit but the fix is now released as 6.6.0 on npm along with the changes to collection.import.

@sshillyer
Copy link
Author

Sorry to resurrect this, I lost track of it as I was moved on to another project and we sidelined our project.

I have created a minimal example project that recreates my problem:
https://github.com/sshillyer/arangojs-min-error

It involves only a couple of lines of code and only typescript and arangojs modules. I tried using @types/node in the example as well but also has the problems aforementioned.

Open to ideas! Thanks

@pluma
Copy link
Contributor

pluma commented Nov 2, 2018

Make sure to add dom to the lib section in your tsconfig.json. See #577 for additional details.

I'll add a sentence about that to the README to help other users in the future.

@pluma
Copy link
Contributor

pluma commented Nov 2, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A code defect that needs to be fixed. Typescript This issue is about Typescript.
Projects
None yet
Development

No branches or pull requests

2 participants