Add support for BigInt
See original GitHub issueBigInt is one of the standard built-in types in Typescript. At the moment TypedJSON does not support parsing BigInt out of the box. The underlying problem is also that the standard JSON.parse()
does not support parsing bigint - it coerces bigints into number
, which loses precision.
Currently BigInt
can be parsed by TypedJSON via custom de-/serializes and use of an external JSON parser that supports bigint parsing: https://github.com/sidorares/json-bigint.
Suggestion: add support for BigInts to TypedJSON
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
BigInt - JavaScript - MDN Web Docs
BigInt values represent numeric values which are too large to be represented by the number primitive.
Read more >New in Access 2016—Large Number (BigInt) support - Microsoft
When creating new local tables or editing existing ones, Access now allows users to add fields that store BigInt numbers.
Read more >BigInt - The Modern JavaScript Tutorial
BigInt is a special numeric type that provides support for integers of arbitrary length. ... Addition, c = a + b, c =...
Read more >Is there anyway for IE11 to support BigInt? - Stack Overflow
No there is not. There were never plans for it to add Support for Internet Explorer and as Micrsoft drops support for IE...
Read more >BigInt | Can I use... Support tables for HTML5, CSS3, etc
BigInt · Global · Chrome · Edge * · Safari · Firefox · Opera · IE · Chrome for Android.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Personally, I think
mapTypes
is not the best way to do it. It can only acceptBigInt
constructor, so all theBigInt
fields should be declared like that (as shown in README):But assigning
BigInt
type to the class field means it’ll be of typeBigInt
at compile-time, not a primitivebigint
type. So, you can’t really use it in any calculations, because code like this will not compile:So, to overcome this issue, I usually use something like this in my code:
I see where you’re coming from but do not believe we would support this anytime soon due to the need for using a custom
JSON.parse
method. Using the third-party JSON.parse library you mentioned has the following issues:BigInt
. If, for example, decimal is approved we will need another custom JSON.parse library for that or need to maintain our own. I would much prefer using the JSON.parse method provided by the browser or Node. Especially since it is likely to be faster and better tested.Another thing to note is that while
BigInt
is a built-in type in TypeScript, it appears to only be available when targetingesnext
.Presenting precise numbers in JavaScript has long been an issue and will probably remain one for a while longer. Perhaps https://github.com/tc39/proposal-json-parse-with-source will solve these problems. In the meantime I strongly recommend to represent numbers outside of IEEE 754 precision as strings in JSON. If strings are used then the example shown above will preserve precision.
I’ve used the decimals represented by strings approach myself in applications that deal with money and can confirm it works perfectly. PayPal uses it too.