-
Notifications
You must be signed in to change notification settings - Fork 57
BigInt.prototype.toString() should return literal-form with 'n' suffix and constructor should accept it as well, e.g. BigInt('1234n') #160
Comments
@ljharb thx for reference |
I'm not sure what you're getting at with JSON. It still seems potentially error-prone to turn certain magic strings into BigInt. There are some libraries which handle BigInt JSON serialization, such as granola. I'd recommend using their pattern, of interpreting certain objects as BigInt, rather than certain strings. |
re @littledan's comment in issue #5: how about just additionally allowing 'n' suffix in constructor, e.g. BigInt('1234n')? it wouldn't adversely affect compatibility for what's currently-shipped, and makes it easier to implement custom parsers for JSON-stringified form '{"aa":"1234n"}' (for those of us wanting idiot-proof format that cannot be accidentally parsed by Number and parseInt). also curious, what magic strings are problematic? do u mean base64 mistaken as bigint? as thats not really the point. the point is to make BigInt distinguishable from Number in JSON (for both humans and machines). |
I don't want to just allow any arbitrary string in just because we can think of a case where it's convenient. That would be a never-ending process--you can always think of reasons why something would be convenient. In this case, it's sort of a category error: the If you know you have a string which represents a BigInt ending in
By magic strings being problematic, I mean distinguishing whether |
Note that JSON parsing and stringification is unrelated to the |
Note that the trailing "n" doesn't prevent such accidents:
|
For context: Don't use |
I'll say no trailing "n" for "BigInt#toString", because that's generally used for humans. But for "BigInt#toSource", it could desired. |
toSource isn’t in the standard. |
@kaizhu256 The rationale has been extensively discussed above. Is there anything more we should do in this issue? |
thx for insight on irrelevance of 'n'. i still think its possible to have guidance for JSON-serialization before stage-4, though. closing this issue. |
from an integration-perspective, its less error-prone to revive/parse BigInt from JSON if its visually distinct from Number. the 'n' suffix can provide that distinction.
otherwise, we end up with bad design-patterns, like being unable to easily debug/distinguish BigInt from Number when inspecting JSON-text. e.g.:
The text was updated successfully, but these errors were encountered: