dethcrypto / truffle-typings

Typescript typings for Truffle
11 stars 9 forks source link

Declare BN and Web3 types and use them #7

Closed sebastianst closed 5 years ago

sebastianst commented 5 years ago

We can use typescript's import() to declare external types, which I apply to BN from bn.js and Web3 from web3.

While @types/web3 is certainly not perfect, it is better than not having any types at all and they work reasonably well (for me). It can always be overwritten with declare const web3: any by the user, I think.

I also changed the TransactionDetails to have from as an optional and the other fields to be BNs. Note in particular that web3's value doesn't accept a number any more. To convert strings and numbers to a BN, use web3.utils.toBN().

It is also possible to use the web3 node module directly. However, then the two declarations have to be changed to:

declare type BN = import("web3-utils").BN;
declare type Web3 = import("web3").default;

There is also currently a bug in web3.js's types, making it impossible to compile web3-utils, because it doesn't correctly import/require bn.js. It will be fixed this week.

Also relates to https://github.com/ethereum-ts/TypeChain/issues/136

krzkaczor commented 5 years ago

Hey @sebastianst,

Current version of truffle-typings works with truffle@4.

This PR introduces support for @5 (which uses web3) is this correct?

sebastianst commented 5 years ago

Yes that's correct. To be precise: node v11.9.0, web3 1.0.0-beta.43, typescript 3.3.1, truffle v5.0.2 and truffle-typings 1.0.6.

krzkaczor commented 5 years ago

Thanks for your contribution 🎉 (and sorry for waiting so long)