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

Support for JavaScript BigInt in BigNumber Constructor #376

Open
Moicky opened this issue Oct 20, 2024 · 7 comments
Open

Support for JavaScript BigInt in BigNumber Constructor #376

Moicky opened this issue Oct 20, 2024 · 7 comments

Comments

@Moicky
Copy link

Moicky commented Oct 20, 2024

Currently, the constructor of the BigNumber library is complaining when using the native JavaScript BigInt type in TypeScript. When trying to create a BigNumber instance using a BigInt value (e.g., new BigNumber(100n)), it throws an error or does not behave as expected.

Expected Behavior
I would expect that the constructor of BigNumber should accept JavaScript's native BigInt type and initialize a BigNumber instance accordingly. For example:

const bigNumber = new BigNumber(100n);

Actual Behavior
Passing a BigInt to the constructor currently results in an error or is not handled appropriately. It seems that the library does not recognize BigInt as a valid input type.

Rationale
JavaScript introduced the BigInt type in ES2020 to handle arbitrarily large integers, which aligns well with the goals of the BigNumber.js library. Allowing the BigNumber constructor to accept BigInt would simplify the interaction between native JavaScript types and the BigNumber library, making it easier for developers to work with large numbers.

Currently, developers need to manually convert BigInt to string (or other types) before passing them to BigNumber, which feels unnecessary and counterintuitive, given the overlapping functionality between BigInt and BigNumber.

For example, instead of doing:

const bigNumber = new BigNumber(100n.toString());

It should be possible to do:

const bigNumber = new BigNumber(100n);

Proposal
Update the constructor of BigNumber to accept the BigInt type and convert it internally to the appropriate format for BigNumber. This change would improve usability and modernize the library to align with newer JavaScript features.

Additional Information
JavaScript BigInt documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Let me know if this feature could be considered or if there's a workaround for this in the current version. Thank you!

@Moicky Moicky changed the title Suggestion: support bigint in constructor Support for JavaScript BigInt in BigNumber Constructor Oct 20, 2024
@MikeMcl
Copy link
Owner

MikeMcl commented Oct 21, 2024

What version are you using, or perhaps you are using TypeScript, because passing a BigInt to the constructor works for me?

@Moicky
Copy link
Author

Moicky commented Oct 21, 2024

This is most likely a TS issue yes.

I think the best solution would be to update the types since JS is known for casting variable types in weird ways. It should be ensured that BigInts are treated properly

@MikeMcl
Copy link
Owner

MikeMcl commented Oct 21, 2024

I have just merged and pushed a previous pull request that added support for BigInt to the types. I will publish later this week after deciding whether to accept another PR that adds a toBigInt method.

Anyway, thanks for the prompt on this.

@NoahAndrews
Copy link

Looks like you merged and then reverted the toBigInt PR? I don't see that in the history though...

Is there still a chance that feature makes it in?

@Moicky
Copy link
Author

Moicky commented Oct 27, 2024

Looks like you merged the PR.

Could you publish this version to npm? Would love to install it asap 😄

@Moicky
Copy link
Author

Moicky commented Oct 28, 2024

I think you'd want to use bigint as the type and not the constructor of the type (BigInt)

I published a version with this fix since I needed this asap and was getting tired of always converting BigInts to string 😂

@MikeMcl
Copy link
Owner

MikeMcl commented Oct 29, 2024

@Moicky

Okay, thanks for letting me know.

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

No branches or pull requests

3 participants