Add type-safe publish overloads and fix unsafe as casts#193
Merged
baelter merged 4 commits intomessage-codec-registryfrom Mar 13, 2026
Merged
Add type-safe publish overloads and fix unsafe as casts#193baelter merged 4 commits intomessage-codec-registryfrom
baelter merged 4 commits intomessage-codec-registryfrom
Conversation
- Add overloaded signatures to publish/call methods: without contentType only Body types are accepted; with contentType any value is allowed (codec will serialize it) - Fix unsafe `as` casts in AMQPRPCClient and AMQPRPCServer by properly using the typed Uint8Array result from serializeAndEncode() - Make AMQPParser generic with TIn/TOut type parameters for custom parser type safety (backward compatible via defaults) - Make AMQPMessage.parse() generic so callers can specify return type - Export Body type from index.ts for user reference https://claude.ai/code/session_01HmLU5GkXfG8RLRE3qJXUku
AMQPParser<TIn, TOut> now requires explicit type arguments. Updated all internal usages, registry methods, and tests accordingly. Also added publish overloads to AMQPSession.rpcCall. https://claude.ai/code/session_01HmLU5GkXfG8RLRE3qJXUku
| * @returns `this` for chaining | ||
| */ | ||
| async publish(body: Body, options?: ExchangePublishOptions): Promise<AMQPExchange> | ||
| async publish(body: unknown, options: ExchangePublishOptions & { contentType: string }): Promise<AMQPExchange> |
Contributor
There was a problem hiding this comment.
I don't think this will help with type safety as body can still be unknown.
e.g
exchange.publish(()=> "hello") will compile but it will not become a helpful result.
… type AMQPParser<TIn, TOut> collapsed to AMQPParser<T> so serialize input matches parse output. Publish overloads now use Serializable instead of unknown, rejecting functions and other non-serializable values.
The <T> param was just casting with `as T`, providing no real type safety. Return unknown and let callers validate/narrow.
baelter
added a commit
that referenced
this pull request
Mar 16, 2026
* Add type-safe publish overloads and fix unsafe `as` casts - Add overloaded signatures to publish/call methods: without contentType only Body types are accepted; with contentType any value is allowed (codec will serialize it) - Fix unsafe `as` casts in AMQPRPCClient and AMQPRPCServer by properly using the typed Uint8Array result from serializeAndEncode() - Make AMQPParser generic with TIn/TOut type parameters for custom parser type safety (backward compatible via defaults) - Make AMQPMessage.parse() generic so callers can specify return type - Export Body type from index.ts for user reference https://claude.ai/code/session_01HmLU5GkXfG8RLRE3qJXUku * Remove backward-compat defaults from AMQPParser generic params AMQPParser<TIn, TOut> now requires explicit type arguments. Updated all internal usages, registry methods, and tests accordingly. Also added publish overloads to AMQPSession.rpcCall. https://claude.ai/code/session_01HmLU5GkXfG8RLRE3qJXUku * Address review: single type param for AMQPParser, narrow publish body type AMQPParser<TIn, TOut> collapsed to AMQPParser<T> so serialize input matches parse output. Publish overloads now use Serializable instead of unknown, rejecting functions and other non-serializable values. * Drop fake generic on Message#parse, return Promise<unknown> The <T> param was just casting with `as T`, providing no real type safety. Return unknown and let callers validate/narrow. --------- Co-authored-by: Claude <noreply@anthropic.com>
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.
Summary
contentType, onlyBodytypes are accepted. WithcontentType,Serializablevalues (objects, arrays, numbers, booleans) are allowed. Functions and other non-serializable types are rejected at compile time.AMQPParser<T>single generic param: Serialize input matches parse output. Custom parser authors specify one type. Built-in parsers:AMQPParser<string>(plain),AMQPParser(JSON, defaults tounknown).AMQPMessage.parse()returnsPromise<unknown>: No fake<T>generic that just casts withas T. Callers validate/narrow the result themselves.ascasts in RPC client/server: Uses typedUint8ArrayfromserializeAndEncode()directly.BodyandSerializabletypes fromindex.ts.Test plan
npx tsc --noEmitpassesq.publish({foo: 1})errors withoutcontentTypeq.publish({foo: 1}, { contentType: "application/json" })compilesq.publish(() => "hello", { contentType: "application/json" })errors