- Typed messages. One type describes every message a side can send or receive. A wrong name or payload is a compile error, rather than a silent runtime bug.
- Requests with responses.
createTypedRpcChanneladds request/response calls on top of plain messages, with abort and timeout support. - Pluggable transports.
EventTargetandpostMessagetransports ship with the package. A custom transport is a small object withonandemitfunctions. - No runtime dependencies. The core starts at 341 B.
import { createTypedChannel } from "typed-channel"
import { createPostMessageTransport } from "typed-channel/transports/postMessage"
type Inbound = { status: { code: number } }
type Outbound = { fetch: { id: string }; cancel: never }
const channel = createTypedChannel(createPostMessageTransport<Inbound, Outbound>(worker))
channel.on("status", ({ code }) => console.log(code))
channel.emit("fetch", { id: "123" })
Guides for messages, requests, and transports are at typed-channel.psdcoder.dev.