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][web3-utils] Use bn.js directly for class BN instead of wrapper #2350

Closed
sebastianst opened this issue Feb 8, 2019 · 3 comments · Fixed by #2529
Closed

[types][web3-utils] Use bn.js directly for class BN instead of wrapper #2350

sebastianst opened this issue Feb 8, 2019 · 3 comments · Fixed by #2529
Labels
Types Incorrect or missing types

Comments

@sebastianst
Copy link

sebastianst commented Feb 8, 2019

Expected behavior

Let BN denote the class as exported from web3-utils, not bn.js.
Assigning results of BN methods, like add(), to variables of type BN should work.

Actual behavior

Doesn't compile, throws typescript errors.

Steps to reproduce the behavior with error logs

The code

import { BN } from "web3-utils";
let a = toBN(0); // or new BN(0)
let b: BN;
b = a.add(toBN(1));

gives error

error TS2741: Property 'super' is missing in type 'BN' but required in type 'BN'.

71 b = a.add(toBN(1));
   ~

  node_modules/web3-utils/types/index.d.ts:73:5
    73     super(number: number | string | number[] | Buffer | BN, base?: number | 'hex', endian?: 'le' | 'be'): BigNumber;
           ~~~~~
    'super' is declared here.

So right now I do arithmetics with a.add(toBN(1)) as BN hacks.

Now I could instead do import BN = require('bn.js'); but then I get another error when passing those BNs into, e.g., web3.utils.soliditySha3:

error TS2345: Argument of type 'BN' is not assignable to parameter of type 'Mixed'.
  Property 'super' is missing in type 'BN' but required in type 'BN'.

Background

The web3-utils type declarations internally first do

import BigNumber = require('bn.js');

(which in itself is confusing, because bn.js is usually imported as BN and BigNumber is used for bignumber.js) and then declares and exports a wrapper class

export class BN extends BigNumber {
    constructor(number: number | string | number[] | Buffer | BN, base?: number | 'hex', endian?: 'le' | 'be');
    super(number: number | string | number[] | Buffer | BN, base?: number | 'hex', endian?: 'le' | 'be'): BigNumber;
}

Why is that? Why doesn't web3 just use bn.js directly?

This whole BN situation is a bit confusing, see also #2171

Versions

Tested with node v11.9.0 web3 version 1.0.0-beta.43 and typescript 3.3.1 in a truffle v5.0.2 project.

@nivida nivida added the Types Incorrect or missing types label Feb 8, 2019
@nivida
Copy link
Contributor

nivida commented Feb 8, 2019

Thanks for submitting this issue I'll fix this asap.

Edit:
Could you update the issue for being align with the issue template?

@sebastianst
Copy link
Author

Thanks for fixing. Will update the issue.

@sebastianst
Copy link
Author

Found an even easier example with clone():

let a = toBN(0);
let b: BN = a.clone();

gives the same error.

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

Successfully merging a pull request may close this issue.

2 participants