fix: support BigInt64Array and BigUint64Array serialization - #353
Merged
Skn0tt merged 1 commit intoJun 18, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes SuperJSON’s handling of BigInt64Array and BigUint64Array by ensuring their elements are serialized into JSON-safe values and correctly revived on deserialization, aligning their behavior with existing typed-array support.
Changes:
- Serialize BigInt typed-array elements as decimal strings (since JSON cannot represent
bigint). - Add guarded constructor lookup for
BigInt64Array/BigUint64Arrayto enable correct deserialization. - Add a round-trip fixture test covering precision-critical BigInt values and the
BigUint64Arraymax.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/transformer.ts | Adds BigInt typed-array handling in the typed-array transform/untransform logic (string encoding + constructor mapping). |
| src/is.ts | Expands typed-array typings to include BigInt typed arrays for internal narrowing consistency. |
| src/index.test.ts | Adds a fixture ensuring BigInt typed arrays serialize to JSON-safe strings and revive to the correct typed-array classes with exact values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
14 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
BigInt64ArrayandBigUint64Arrayare detected as typed arrays byisTypedArray(they passArrayBuffer.isViewand aren'tDataView), so superjson annotates them as['typed-array', 'BigInt64Array']and tries to round-trip them. But the round-trip is broken in two ways:serialize/stringifythrows — the transform produces an array of rawbigintelements, whichJSON.stringifycannot represent:TypeError: Do not know how to serialize a BigInt.deserializethrows —constructorToNameonly lists the numeric typed arrays, so the BigInt variants hitError: Trying to deserialize unknown typed array.Reproduction on
main:Fix
bigintisn't valid JSON, so (mirroring how the existing code storesNaN/Infinityas strings) BigInt typed-array elements are serialized as decimal strings and parsed back withBigInt(...)on deserialization. A smallbigIntConstructorToNamelookup maps the two BigInt typed-array constructors back to their classes.The constructor registration is guarded with
typeof BigInt64Array !== 'undefined', consistent with the existing BigInt-polyfill defensiveness intransformer.ts, so environments without these globals are unaffected.Serialized form (now JSON-safe):
{ "json": ["1", "2", "3"], "meta": { "values": [["typed-array", "BigInt64Array"]], "v": 1 } }Tests
Added a
works with BigInt typed arrayscase to thestringify & parsefixtures (which round-trips throughJSON.parse(JSON.stringify(...)), so it also covers thestringifypath). It verifies the serialized shape, the type annotations, that values revive to the correct typed-array class, and exact element equality — including values that would lose precision asnumber(9007199254740993n) andBigUint64Array's max (18446744073709551615n).The new test fails on
main(Do not know how to serialize a BigInt) and passes with this change. Full suite (pnpm test) andtscbuild both pass.Greptile Summary
This PR fixes broken serialization of
BigInt64ArrayandBigUint64Arrayby converting theirbigintelements to/from decimal strings during JSON encoding — mirroring the existingNaN/Infinitystring strategy for numeric typed arrays.src/transformer.ts: AbigIntConstructorToNamelookup (guarded bytypeof BigInt64Array !== 'undefined') maps constructor names to their classes; the serialize path callsn.toString()for bigint elements, and the deserialize path callsBigInt(n)and reconstructs the correct typed-array subclass.src/is.ts: AddsBigIntTypedArrayConstructorand widensTypedArrayto includeBigInt64Array/BigUint64Arrayinstances, keeping the type definitions consistent with the new runtime support.src/index.test.ts: Adds a fixture that round-trips both array types, including a value that would lose precision as anumber(9007199254740993n) andBigUint64Array's maximum (18446744073709551615n).Confidence Score: 4/5
Safe to merge; the fix is narrowly scoped to the two BigInt typed-array classes and does not touch existing numeric typed-array paths.
The serialization and deserialization logic is correct and follows the established NaN/Infinity string pattern. The
bigIntConstructorToNamelookup is properly guarded for environments without BigInt typed arrays. Tests cover precision-critical values and the full round-trip through JSON.stringify/parse.The deserialization branch in
src/transformer.ts(lines 254–258) is the most sensitive change — worth a quick read to confirm theBigInt(n)call aligns with all expected input shapes.Important Files Changed
BigIntTypedArrayConstructortype and expandsTypedArrayto include BigInt64Array and BigUint64Array instances — clean, minimal type-level change.bigIntConstructorToNamelookup and updatestypedArrayRuleto serialize bigint elements as strings and deserialize them back withBigInt(n). Logic is sound; naming follows existing convention.Sequence Diagram
sequenceDiagram participant Caller participant typedArrayRule participant JSON Note over Caller,JSON: Serialize BigInt64Array([1n, 2n, -3n]) Caller->>typedArrayRule: transform(BigInt64Array) typedArrayRule->>typedArrayRule: "[...v].map(n => n.toString()) for bigint" typedArrayRule-->>Caller: ["1", "2", "-3"] + annotation ["typed-array", "BigInt64Array"] Caller->>JSON: JSON.stringify(["1","2","-3"]) JSON-->>Caller: valid JSON Note over Caller,JSON: Deserialize Caller->>JSON: JSON.parse(...) JSON-->>Caller: ["1", "2", "-3"] Caller->>typedArrayRule: untransform(["1","2","-3"], ["typed-array","BigInt64Array"]) typedArrayRule->>typedArrayRule: "bigIntConstructorToName["BigInt64Array"] -> BigInt64Array" typedArrayRule->>typedArrayRule: "v.map(n => BigInt(n)) -> [1n, 2n, -3n]" typedArrayRule->>typedArrayRule: new BigInt64Array([1n, 2n, -3n]) typedArrayRule-->>Caller: BigInt64Array([1n, 2n, -3n])Reviews (1): Last reviewed commit: "fix: support BigInt64Array and BigUint64..." | Re-trigger Greptile