Skip to content

Conversation

@Refzlund
Copy link
Owner

Summary

This PR enhances the toJSON() method with an optional iterator parameter that controls whether Symbol.iterator is included in the serialized output.

Changes

src/result.ts

  • Enhanced toJSON() signature: Added optional generic parameter TIterator extends boolean = true to control iterator inclusion
  • New Result.PlainJSON type: A clean JSON type without Symbol.iterator or Result.symbol, useful for serialization to external systems
  • Conditional return type: toJSON(false) returns Result.PlainJSON, toJSON() or toJSON(true) returns Result.JSON (with iterator)
  • Refactored Result.JSON type: Now extends PlainJSON to avoid code duplication

tests/serialization.test.ts

  • Added comprehensive tests for the new iterator parameter
  • Tests cover both Ok and Err results with toJSON(false)
  • Type tests verify correct type inference for Result.JSON vs Result.PlainJSON
  • Tests verify Symbol.iterator presence/absence based on parameter

Use Case

When serializing Results for transport (e.g., HTTP responses, message queues), you often want plain JSON without symbols:

// Before: Had to manually strip symbols or work around it
const response = result.toJSON()
delete response[Symbol.iterator]

// After: Clean and type-safe
const response = result.toJSON(false)
// => { ok: true, value: ... } or { ok: false, code: ..., message: ... }

Breaking Changes

None. The default behavior (toJSON()) remains unchanged and returns Result.JSON with the iterator included.

@changeset-bot
Copy link

changeset-bot bot commented Nov 25, 2025

🦋 Changeset detected

Latest commit: c16b748

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
xult Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Refzlund Refzlund merged commit 2f13e94 into release Nov 25, 2025
1 check passed
@Refzlund Refzlund deleted the feat-allow-plainjson branch November 25, 2025 20:43
@github-actions github-actions bot mentioned this pull request Nov 25, 2025
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.

2 participants