Skip to content

Preserve collection toJSON return types - #242

Merged
endel merged 1 commit into
colyseus:masterfrom
snowyukitty:fix/collection-to-json-types
Oct 12, 2026
Merged

endel merged 1 commit into
colyseus:masterfrom
snowyukitty:fix/collection-to-json-types

Conversation

@snowyukitty

Copy link
Copy Markdown
Contributor

Direct ArraySchema.toJSON() and MapSchema.toJSON() calls currently return any[] and any, allowing invalid assignments and losing the serialized element shape in typed UI state.

Annotate the existing methods with arrays/string-keyed records of each element's inferred toJSON() return type, or the element type when it has no serializer. This preserves primitive and literal values, recursively serialized Schema data, and custom toJSON() overrides. Keep the existing ToJSON utility, Schema/assign behavior, and runtime implementation unchanged.

Extend the strict emitted-declaration fixtures with positive and negative checks for primitive values, codec-named string literals, Schema data versus class methods, nested/custom serialization, union/default values, recursive collections, and native collection interfaces. The latter two cover the compatibility problems discussed in #174.

Validation on Node.js 20 and 24:

  • npm test: 1,043 passing, one existing pending; all six TypeScript 5/7 compiler configurations pass.
  • npm run build and npm run test:exports pass.
  • The exact new consumer fixtures fail against the unmodified serializers on both compilers; the four unaffected configurations pass.
  • All 6 emitted JavaScript runtime artifacts are byte-identical to the unmodified build.

Fixes #162

@endel
endel merged commit 376abb5 into colyseus:master Oct 12, 2026
endel added a commit that referenced this pull request Oct 12, 2026
endel added a commit that referenced this pull request Nov 10, 2026
Port of master 50d1576 (#242).

Assisted-by: Claude Opus 5.5
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.

TypeScript: toJSON() return type on MapSchema / ArraySchema

2 participants