Skip to content

fix(router-core): properly handle serialization of readonly maps/sets at type level - #7960

Open
pawelblaszczyk5 wants to merge 1 commit into
TanStack:mainfrom
pawelblaszczyk5:readonly-set-maps-serialization
Open

fix(router-core): properly handle serialization of readonly maps/sets at type level#7960
pawelblaszczyk5 wants to merge 1 commit into
TanStack:mainfrom
pawelblaszczyk5:readonly-set-maps-serialization

Conversation

@pawelblaszczyk5

@pawelblaszczyk5 pawelblaszczyk5 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Currently Set and Maps are properly serialized via Seroval. This is reflected on the type level so Set and Maps are properly validated for containing only serializable outputs for server functions. However, this doesn't properly work with ReadonlySet and ReadonlyMap variants:

image image

WIth the patch from this PR applied it works correctly:

image

Besides validating in my project I've also added tests to existing spec that tested type level serialization behavior to ensure all of these works properly

Summary by CodeRabbit

  • Bug Fixes

    • Improved serialization type validation for readonly maps and sets.
    • Readonly collection contents are now checked recursively, including keys, values, and elements.
    • Invalid nested values produce clearer serialization errors.
  • Tests

    • Added coverage for valid and invalid readonly maps and sets.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The serializer now supports type-level validation for ReadonlySet and ReadonlyMap. Tests cover valid and invalid nested types. A patch changeset records the fix.

Changes

Readonly collection serialization

Layer / File(s) Summary
Readonly collection validation and type coverage
packages/router-core/src/ssr/serializer/transformer.ts, packages/router-core/tests/serializer.test-d.ts, .changeset/full-flowers-carry.md
ValidateSerializable recognizes readonly sets and maps. Recursive validators preserve readonly collection types and validate their contained types. Type-level tests cover serializable values and nested SerializationError results. A patch changeset records the correction.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the fix for type-level serialization of readonly maps and sets in router-core.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

? ValidateSerializableSet<T, TSerializable>
: T extends Map<any, any>
? ValidateSerializableMap<T, TSerializable>
: T extends ReadonlyMap<any, any>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I had to swap order of Set and Map detection since ReadonlyMap extends ReadonlySet depending on tsconfig stuff 😄

Image

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/router-core/tests/serializer.test-d.ts`:
- Around line 49-57: Add a type-level test alongside the existing readonly-map
value case using ReadonlyMap<() => void, string>, and assert that
ValidateSerializable replaces the function key with SerializationError<'Function
may not be serializable'> while preserving the string value type.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bcc4f562-ae4e-4767-a350-ccc96c5aed57

📥 Commits

Reviewing files that changed from the base of the PR and between 314098e and 595c1da.

📒 Files selected for processing (3)
  • .changeset/full-flowers-carry.md
  • packages/router-core/src/ssr/serializer/transformer.ts
  • packages/router-core/tests/serializer.test-d.ts

Comment thread packages/router-core/tests/serializer.test-d.ts
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.

1 participant