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 type definitions don't work in NodeJS #2486

Closed
yaram opened this issue Mar 11, 2019 · 7 comments
Closed

TypeScript type definitions don't work in NodeJS #2486

yaram opened this issue Mar 11, 2019 · 7 comments
Labels
Types Incorrect or missing types

Comments

@yaram
Copy link

yaram commented Mar 11, 2019

Description

The TypeScript type definitions do not work when used as a dependency in NodeJS. In the *.cjs.js files used by NodeJS, the old commonjs exports = form is used to export the modules, whereas in the TypeScript bindings the exports are defined to use the ES6 export default form, mapping to exports.default = in the *.esm.js files.

This issue is closely related to #1248.

Error Logs

const web3 = new web3_1.default(null);
             ^

TypeError: web3_1.default is not a constructor

Versions

  • web3.js: 1.0.0-beta.38
  • nodejs: 10.14.1
  • typescript: 3.3.3333
@nivida nivida added the Types Incorrect or missing types label Mar 11, 2019
@Alex1100
Copy link

Have you tried import * as web3 from 'web3'?

@yaram
Copy link
Author

yaram commented Mar 12, 2019

That would work in plain old ES6, but because of the misastched TypeScript type definitions, the TypeScript compiler complains with a type error in the form of

index.ts:3:14 - error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature.

3 const web3 = new Web3(null);
               ~~~~~~~~~~~~~~

@yaram
Copy link
Author

yaram commented Mar 12, 2019

One solution would be to have the TypeScript type definitions export both in the ES6 style and in the commonjs style but as far as I can tell that's not possible.

@joshstevens19
Copy link
Contributor

joshstevens19 commented Mar 12, 2019

Hey thanks for raising this, I will try to recreate your issue tomorrow and take a look. You are just trying to use the types in a standard node app? can you paste how your trying to import it please? that would be awesome.

@joshstevens19
Copy link
Contributor

I think I know what it is - please still supply the above so I can confirm (I only really need the import your trying to do). I can then reply tomorrow once I’m online as I’m UK time and it’s 1am here at the moment. Thanks 👍

@yaram
Copy link
Author

yaram commented Mar 12, 2019

I've uploaded a test package here. Originally I was trying to use web3@^1.0.0-beta.38 from TypeScript in tests.

@joshstevens19
Copy link
Contributor

joshstevens19 commented Mar 12, 2019

Hey @yaram so your issue is due to the mismatch between how commonjs export stuff module.exports = something and how ES6 export stuff export default something.

To fix this it you just have to enable esModuleInterop in your tsconfig compile option, also enable allowSyntheticDefaultImports for typesystem compatibility :

"compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    ....

This then will allow you to import as you would anywhere like:

import Web3 from "web3";

If you look at the compiled source now it does:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const web3_1 = __importDefault(require("web3"));
const web3 = new web3_1.default("https://rinkeby.infura.io/");

The __importDefault is imported now and wraps web3 in it which fixes the issues with it compiling fine and it not running when you tried to.

Will add these instructions to the README so its clear how to get the typing's to work in a commonjs module.

Thanks
Will close now
Josh

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

No branches or pull requests

4 participants