Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
messageSchema()function had an explicitanyreturn type annotation that was stripping away crucial type information needed for Zod'sdiscriminatedUnion()to work properly. This prevented users from creating type-safe discriminated unions for WebSocket message routing, which is essential for complex applications that handle multiple message types.Before (broken):
Solution
Removed the explicit
: ZodObject<any>return type annotation from themessageSchemaimplementation, allowing TypeScript to properly infer the specific return type with all discriminator information preserved.After (working):
Impact
This fix enables powerful type-safe message routing patterns that are critical for production WebSocket applications:
Advanced Message Routing
Enhanced Developer Experience
Changes
zod/schema.ts: Removed explicitanyreturn type to preserve type informationzod/schema.test.ts: Added comprehensive test suite covering:test-discriminated-union.ts: Added verification script for manual testingpackage.json: Version bump to 0.3.4Backwards Compatibility
This change is 100% backwards compatible. Existing code continues to work exactly as before - we're only adding new capabilities, not changing existing behavior.
Testing
The fix includes extensive test coverage:
Run the tests with:
bun test zod/schema.test.ts bun run test-discriminated-union.tsThis fix addresses a critical limitation that was preventing users from leveraging Zod's powerful discriminated union features for building robust, type-safe WebSocket applications.