Web3.js: web3-eth-accounts TypeScript definition file is broken (and/or confusing)

Created on 30 Dec 2019  Â·  9Comments  Â·  Source: ChainSafe/web3.js

Expected behavior

TypeScript definition file should reflect that Accounts is the _default_ export, not a named export.

Actual behavior

TypeScript compiler raises an error when attempting to import Accounts as the default export, but we reach a runtime TypeError if we import Accounts as a named export.

Steps to reproduce the behavior

  1. Try this code snippet in a TypeScript file:
import { Accounts } from 'web3-eth-accounts';

new Accounts() // TypeScript does not complain
// => TypeError: "Accounts is not a constructor"
  1. For comparison:
import Accounts from 'web3-eth-accounts';

new Accounts() // TypeScript complains with: "This expression is not constructable."
// => works at runtime...

Versions

[email protected]

1.x Stale more information needed types

Most helpful comment

Btw.: We can't change this that easy because it would break many already existing projects.

I see this as a _bug_, though. Considering the expected behavior does not currently work, I do not understand how it can be considered a breaking change. Thankfully, there is a workaround using declaration merging:

import { AccountsBase } from 'web3-core';
import Accounts from 'web3-eth-accounts';

declare module 'web3-eth-accounts' {
  export default class Accounts extends AccountsBase {}
}

All 9 comments

Thanks for opening this issue! Have you followed our TS guidance here which does require additional configuration?

Btw.: We can't change this that easy because it would break many already existing projects.

Edit: Sorry, it is possible to change it in a non-breaking manner we would only have to export it named and as default from the same file.

Btw.: We can't change this that easy because it would break many already existing projects.

I see this as a _bug_, though. Considering the expected behavior does not currently work, I do not understand how it can be considered a breaking change. Thankfully, there is a workaround using declaration merging:

import { AccountsBase } from 'web3-core';
import Accounts from 'web3-eth-accounts';

declare module 'web3-eth-accounts' {
  export default class Accounts extends AccountsBase {}
}

We are re-assessing this. There are a few inconsistencies within the deceleration files. Feel free to follow along on #3606 for updates

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

_bump_

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

_bump_

3732 has fixed this issue, please review and merge ,related to #3198

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions. If you believe this was a mistake, please comment.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SCBuergel picture SCBuergel  Â·  3Comments

oktapodia picture oktapodia  Â·  3Comments

TinyWJL picture TinyWJL  Â·  3Comments

mishell-trickster picture mishell-trickster  Â·  3Comments

FradSer picture FradSer  Â·  3Comments