-
Notifications
You must be signed in to change notification settings - Fork 743
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
Comments
What version are you using, or perhaps you are using TypeScript, because passing a BigInt to the constructor works for me? |
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 |
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 Anyway, thanks for the prompt on this. |
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? |
Looks like you merged the PR. Could you publish this version to npm? Would love to install it asap 😄 |
I think you'd want to use I published a version with this fix since I needed this asap and was getting tired of always converting BigInts to string 😂 |
Okay, thanks for letting me know. |
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:
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:
It should be possible to do:
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!
The text was updated successfully, but these errors were encountered: