web3.js 1.x - TypeScript Support

Created on 5 Aug 2019  路  13Comments  路  Source: ChainSafe/web3.js

Description

web3 1.2.0 doesn't work well with TypeScript.

I followed the instructions on how to use web3 with typescript, but I had no luck.

$ tsc --version
Version 3.1.3

Expected behavior

I want to be able to use the type definitions in my editor.

Actual behavior

On vscode I see:
image

If I compile with tsc:

index.ts:1:18 - error TS7016: Could not find a declaration file for module 'web3'. '/tmp/web3test/node_modules/web3/src/index.js' implicitly has an 'any' type.
  Try `npm install @types/web3` if it exists or add a new declaration (.d.ts) file containing `declare module 'web3';`

Steps to reproduce the behavior

  • run tsc --init
  • run yarn add web3
  • Create a file index.ts:
    import Web3 from 'web3'; const web3 = new Web3("ws://localhost:8546");
  • run tsc

Versions

  • web3.js: 1.2.0
  • nodejs: 10.15.0
  • browser: n/a
  • ethereum node: n/a
1.x types

Most helpful comment

Hey guys we had this discussion a while back about bringing them in-house:

https://github.com/ethereum/web3.js/issues/1248

It was then decided it should be done like lodash, redux etc do it.

All 13 comments

2.0 does have type definitions directly in the repository of web3.js. 1.0 had type definitions on DT but they got removed because of the 2.0 (1.0.0-beta.55) types. We could copy the 2.0 types to the 1.0 branch and change them if required. @alcuadrado @gnidan @iurimatias @spalladino

What do we know about https://www.npmjs.com/package/@types/web3? If it's well-maintained, I don't see any issues in installing it alongside web3 to have type definitions, so it's not another piece of code to maintain here.

As I know were they maintained by @Levino and I thought they got removed. @spalladino

@vrde The instructions for 1.x are here.

We could copy the 2.0 types to the 1.0 branch and change them if required.

I think this would be a mistake. Types that hadn't been autogenerated by tsc tend to get out of sync, especially in a project this complex. If incorrect types are included in a package (i.e. in the package.json's types field), you are forced to cast things all the time, which is a mess. This is already happening in 2.x, so let's not repeat it here. I'd rather put them in @types/web3.

Anyway, IMO this project should be migrated to TS. This is one of the few major projects in the Ethereum community that hasn't migrated yet.

I think this would be a mistake. Types that hadn't been autogenerated by tsc tend to get out of sync, especially in a project this complex. If incorrect types are included in a package (i.e. in the package.json's types field), you are forced to cast things all the time, which is a mess. This is already happening in 2.x, so let's not repeat it here. I'd rather put them in @types/web3.

We can keep them there for 1.0. I've moved them to the GitHub repository for exactly the reason for having correct type definitions for each module web3 does provide. We had a bigger discussion about it some months ago and the decision at the end was to have the types here in the repository of web3.js.

Anyway, IMO this project should be migrated to TS. This is one of the few major projects in the Ethereum community that hasn't migrated yet.

Yes, this will happen but there is another focus currently.

Edit:
Closed because the initial issue is answered.

Hey guys we had this discussion a while back about bringing them in-house:

https://github.com/ethereum/web3.js/issues/1248

It was then decided it should be done like lodash, redux etc do it.

@vrde The instructions for 1.x are here.

Watch out, it's @types/web3 not @types/web3.js:

$ npm install --dev @types/web3.js
npm WARN install Usage of the `--dev` option is deprecated. Use `--only=dev` instead.
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types%2fweb3.js - Not found
npm ERR! 404 
npm ERR! 404  '@types/web3.js@latest' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

Okay. Here is the story for everyone to enjoy again:

I said: "Stop maintaining the types here, we can maintain them for you in DT." Then @nivida and @joshstevens19 said: "No, please do not maintain them over there. We will maintain them here. Please deprecate / delete them at DT." Which I did even though I know this is as stupid as it gets but I am not the admin here. So then it turns out that web3 after beta-37 is unstable and incompatible with existing software and it gets dialed back to version beta-37 in order for people to be actually able to use it. But the type defs by @joshstevens19 and their "tests" did come some time after beta-37 and are now gone again. So @nivida s solution is to come back to me and say: "Hey Levin, I thought you were maintaining this."

Nice!

Answer to OP: Typescript is currently not supported for [email protected]. We could bring the types back in DT but then a while later @joshstevens19 will potentially ship types with web3 itself and break everything. I am quite hesitant to get involved in this again.

We have had this conversation already, big projects like redux lodash angular do this for loads of reasons i don't need to go into all of them again as its discussed here https://github.com/ethereum/web3.js/issues/1248.

In terms of 1.0 i'm easy but 2.0 types are working perfectly i have had comments from big blockchain companies in how much it has improved their typescript flow and projects. Here at FunFair its really has made all our complex dapps capture compile time errors, see syntax bugs and write better code, huge positives comments from so many people who have reached out to me. I am not sure why you always come across passive aggressive @Levino but throughout all of this you have only really said unhelpful things, i guess as your the maintainer of @types/web3 and did not agree.

The main motivation of even talking about bringing them in-house was the types in DT was not good enough, i experienced that first had at FunFair and we could not use them. Using DT how would @nivida be able to maintain different types for web3 version 1 and 2 when the interface changes? we can't, this way if someone installed 1.0.2 they get the exact types for 1.0.2 with DT they cant map up a version of web3 with a types in DT version and that is super messy when we're in this situation. Again the main reason why redux brought them in-house.

When we brought the in-house types into web3 we went of the codebase itself and updated the docs as we want and wrote the types from scratch to make them better and correct, surely we want to reuse that hard time spent and put them in 1.0 so everyone can have the same experience?

Anyway we don't need to go over this all again, the project hopefully will be written in TS soon. I think best option as interfaces has not changed would be to put the types into 1.0 so everyone can have them and enjoy the better experience with them in-house instead of going round in cycles, types being maintained in DT for 1.0 and types being maintained in web3 for 2.0 would be a maintainers absolute nightmare @nivida. We can then remove @types/web3 from DT.

@nivida, @joshstevens19 , can we make sure that at least the documentation states on how this whole typescript thing works then?

As a consumer of this repository I expect to do an npm install of Web3.js and have typescript definitions working. Except that they don't.

I would suggest that on the readme something is added in regards to:
If you want typescript use 2.0
If you don't want it, use 1.2.1 and do this and this to get them to work

Also it might be good to add something to the readme on what the differences are between v1 and v2 of the Web3.js library.

I think there're some divergences between web3 v1.2.1 and the Typescript, I had spotted few, but I can remember now exactly, one that is particular different is web3.personal.sign as you can see on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/web3/eth/types.d.ts#L61 and https://web3js.readthedocs.io/en/v1.2.1/web3-eth-personal.html#sign

I think there're some divergences between web3 v1.2.1 and the Typescript, I had spotted few, but I can remember now exactly, one that is particular different is web3.personal.sign as you can see on https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/web3/eth/types.d.ts#L61 and https://web3js.readthedocs.io/en/v1.2.1/web3-eth-personal.html#sign

Agreed I think there are some issues with 1.2.1. I have @types/[email protected] installed and there are some inconsistencies.

Hey guys we have back ported all the typings we did in 2.x branch, the 2.x branch did have a few differents to 1.x so we did try to fix any we spotted when moving across to 1.x. So now on web3 1.2.2 it is shipped with in-house typings which were written and checked against the code itself not the docs.

This means you don鈥檛 need the types lib installed anymore. That @types will be deprecated soon.

If you do spot any issues with them please raise a issue and we get them fixed fast for you. Hopefully this creates a overall better TypeScript experience with web3js as the 2.x branch gives you.

馃憤

Was this page helpful?
0 / 5 - 0 ratings