Skip to content

fix: preserve NaN and Infinity values in typed arrays#349

Merged
Skn0tt merged 1 commit intoflightcontrolhq:mainfrom
kaigritun:fix/typed-array-nan-infinity
Feb 9, 2026
Merged

fix: preserve NaN and Infinity values in typed arrays#349
Skn0tt merged 1 commit intoflightcontrolhq:mainfrom
kaigritun:fix/typed-array-nan-infinity

Conversation

@kaigritun
Copy link
Copy Markdown
Contributor

Summary

Fixes #292

Float64Array and other typed arrays that contain NaN or Infinity values were being corrupted during serialization because JSON.stringify converts these values to null.

Before

const a0 = new Float64Array([NaN, 0, NaN, 1]);
const a1 = stringify(a0);
// {"json":[null,0,null,1],"meta":{"values":[["typed-array","Float64Array"]],"v":1}}
const a2 = parse<Float64Array>(a1);
// Float64Array(4) [0, 0, 0, 1] ❌ NaN values lost

After

const a0 = new Float64Array([NaN, 0, NaN, 1]);
const a1 = stringify(a0);
// {"json":["NaN",0,"NaN",1],"meta":{"values":[["typed-array","Float64Array"]],"v":1}}
const a2 = parse<Float64Array>(a1);
// Float64Array(4) [NaN, 0, NaN, 1] ✅ NaN values preserved

Changes

  • Modified typedArrayRule in transformer.ts to encode NaN, Infinity, and -Infinity as strings during serialization
  • Added corresponding decoding during deserialization
  • Added test case covering Float64Array and Float32Array with special float values

Testing

All existing tests pass. Added a new test case specifically for this fix.

Fixes flightcontrolhq#292

Previously, NaN and Infinity values in typed arrays like Float64Array
were incorrectly serialized as null by JSON.stringify, causing data
loss during deserialization (NaN -> null -> 0).

This change encodes special float values (NaN, Infinity, -Infinity)
as strings within typed arrays during serialization and converts them
back during deserialization, consistent with how these values are
handled elsewhere in superjson.

Added test case covering Float64Array and Float32Array with NaN and
Infinity values.
@kaigritun kaigritun requested a review from Skn0tt as a code owner February 7, 2026 03:20
Copy link
Copy Markdown
Collaborator

@Skn0tt Skn0tt left a comment

Choose a reason for hiding this comment

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

Neat, thanks!

@Skn0tt Skn0tt merged commit bccc082 into flightcontrolhq:main Feb 9, 2026
4 checks passed
@MichaelDeBoey
Copy link
Copy Markdown

I'm not making any claims about the quality of their work, but I wanted to let you know that @kaigritun is a fully-autonomous non-human actor
https://socket.dev/blog/ai-agent-lands-prs-in-major-oss-projects-targets-maintainers-via-cold-outreach

@Skn0tt
Copy link
Copy Markdown
Collaborator

Skn0tt commented Feb 17, 2026

Thanks for bringing it up. That explains why I saw another PR of them in a different repo I maintain the same day :D This change was pretty flawless!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NaN values in Float64Array are incorrectly serialised as null

3 participants