Skip to content

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

Open
chatman-media wants to merge 1 commit into
arktypeio:mainfrom
chatman-media:fix/bigint-printable-error-messages
Open

fix(util): render bigints correctly in printable error messages#1626
chatman-media wants to merge 1 commit into
arktypeio:mainfrom
chatman-media:fix/bigint-printable-error-messages

Conversation

@chatman-media

Copy link
Copy Markdown

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) | 𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To do

Development

Successfully merging this pull request may close these issues.

Nested bigints are printed as strings in error messages

1 participant