Skip to content

Add type-safe publish overloads and fix unsafe as casts#193

Merged
baelter merged 4 commits intomessage-codec-registryfrom
claude/add-missing-types-NH3pn
Mar 13, 2026
Merged

Add type-safe publish overloads and fix unsafe as casts#193
baelter merged 4 commits intomessage-codec-registryfrom
claude/add-missing-types-NH3pn

Conversation

@baelter
Copy link
Copy Markdown
Member

@baelter baelter commented Mar 12, 2026

Summary

  • Overloaded publish signatures: Without contentType, only Body types are accepted. With contentType, Serializable values (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 to unknown).
  • AMQPMessage.parse() returns Promise<unknown>: No fake <T> generic that just casts with as T. Callers validate/narrow the result themselves.
  • Fixed unsafe as casts in RPC client/server: Uses typed Uint8Array from serializeAndEncode() directly.
  • Exported Body and Serializable types from index.ts.

Test plan

  • npx tsc --noEmit passes
  • All codec registry and session tests pass
  • Verify q.publish({foo: 1}) errors without contentType
  • Verify q.publish({foo: 1}, { contentType: "application/json" }) compiles
  • Verify q.publish(() => "hello", { contentType: "application/json" }) errors

- 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
@baelter baelter marked this pull request as draft March 12, 2026 19:01
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
Comment thread src/amqp-codec-registry.ts Outdated
Comment thread src/amqp-exchange.ts Outdated
* @returns `this` for chaining
*/
async publish(body: Body, options?: ExchangePublishOptions): Promise<AMQPExchange>
async publish(body: unknown, options: ExchangePublishOptions & { contentType: string }): Promise<AMQPExchange>
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.

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.

baelter added 2 commits March 13, 2026 08:24
… 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 baelter marked this pull request as ready for review March 13, 2026 08:37
@baelter baelter merged commit 6dab1b6 into message-codec-registry Mar 13, 2026
@baelter baelter deleted the claude/add-missing-types-NH3pn branch March 13, 2026 08:37
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>
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.

3 participants