Skip to content

fix(util): render bigints correctly in printable error messages - #1626

Merged
ssalbdivad merged 4 commits into
arktypeio:mainfrom
chatman-media:fix/bigint-printable-error-messages
Oct 1, 2026
Merged

ssalbdivad merged 4 commits into
arktypeio:mainfrom
chatman-media:fix/bigint-printable-error-messages

Conversation

@chatman-media

Copy link
Copy Markdown
Contributor

Summary

Closes #1477

When bigint values appear nested inside arrays or objects, printable() emits
them as quoted strings (e.g. ["1n","0n","1n"]) instead of the correct
unquoted bigint notation ([1n,0n,1n]). Top-level bigints (e.g.
printable(1n)) were already correct.

Root cause

The default path in printable() serialised values with _serialize() then
passed the result to JSON.stringify():

JSON.stringify(_serialize(o, printableOpts, []), null, opts?.indent)

_serialize() converts bigints to plain strings ("1n") because
printableOpts defines no onBigInt handler. JSON.stringify then wraps
those strings in quotes, producing "\"1n\"" inside arrays/objects.

Fix

Replace JSON.stringify(_serialize(...)) with a new _printableStringify
helper that builds the JSON-like string directly. It emits Xn for bigints
(same as serializePrimitive) and falls back to JSON.stringify for all other
scalar types, preserving the existing output format exactly:

  • string keys are always quoted ({"a":1}, not {a: 1})
  • indent/cycle/symbol/function/Date/toJSON handling is unchanged
  • quoteKeys: false still delegates to the existing stringifyUnquoted path

Test evidence

Two new tests in ark/util/__tests__/printable.test.ts:

✔ bigint in array   →  [1n,0n,1n]   (was ["1n","0n","1n"])
✔ bigint in object  →  {"a":1n,"b":2n}  (was {"a":"1n","b":"2n"})

All 128 util tests pass. Schema and type package tests unchanged.

When bigint values appeared nested inside arrays or objects, `printable()`
would output them as quoted strings (e.g. `["1n","0n","1n"]`) instead of
the correct unquoted bigint notation (`[1n,0n,1n]`).

Root cause: the default path in `printable()` used `JSON.stringify` on the
output of `_serialize()`, which converted bigints to plain strings before
`JSON.stringify` could see them, causing the strings to be double-quoted.

Fix: replace `JSON.stringify(_serialize(...))` with a new `_printableStringify`
helper that builds the JSON-like string directly, emitting `Xn` for bigint
values instead of converting them to strings first.

Closes arktypeio#1477

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed changes — fixes bigint rendering inside arrays/objects in printable(), replacing a _serialize + JSON.stringify round-trip with a direct string builder that emits Xn for bigints inline.

  • Add _printableStringify helper — a recursive JSON-like string builder that bypasses _serialize's lossy bigint→string conversion and JSON.stringify's quoting of those strings.
  • Add bigint-in-array and bigint-in-object tests — two focused snapshot tests covering the fixed paths.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

chatman-media and others added 3 commits June 26, 2026 15:26
Rather than reimplementing JSON serialization, keep the existing
_serialize + JSON.stringify path and have printableOpts serialize
bigints with a private-use marker prefix, then strip the quotes
JSON.stringify adds around marked values. This fixes arktypeio#1477 without
changing how functions, sparse arrays or toJSON results print.

Adds the issue's repro as an error message test.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@ssalbdivad
ssalbdivad merged commit 89ee4c2 into arktypeio:main Oct 1, 2026
6 checks passed
@ssalbdivad ssalbdivad mentioned this pull request Oct 1, 2026
4 tasks done
ssalbdivad added a commit that referenced this pull request Oct 1, 2026
Bumps all publishable packages and documents the changes merged since 2.2.6:

- fix `this[]` in self-referential object types (#1618, fixes #1406,
  @yharaskrik)
- parse string.date.epoch.parse input as milliseconds (#1669, @breken-ai)
- keep uppercase UUIDs valid in string.uuid JSON Schema (#1670,
  @breken-ai)
- render nested bigints correctly in error messages (#1626, fixes #1477,
  @chatman-media)
- release unused parse ids from the global registry (#1673, fixes #1584
  for equivalent types, @isaacwasserman)
- fix crashes on Hermes and Turbopack (#1674, fixes #1645 and #1643)

#1590 only adds tests, so it has no changelog entry.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
ssalbdivad pushed a commit that referenced this pull request Oct 27, 2026
printable serialized a nested undefined as the string "undefined", so
JSON.stringify quoted it and {a: undefined} printed the same as
{a: "undefined"}. Mark it the same way #1626 marks bigints and strip
the quotes.

Co-authored-by: youdie006 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done (merged or closed)

Development

Successfully merging this pull request may close these issues.

Nested bigints are printed as strings in error messages

2 participants