# typed-channel

> Type-safe messages and requests between workers, iframes, tabs, and Figma plugins.

- URL: https://evilmartians.com/opensource/typed-channel
- GitHub: https://github.com/psd-coder/typed-channel
- Website: https://typed-channel.psdcoder.dev/
- Authors: Pavel Grinchenko

---

typed-channel is a type-safe communication channel for sending messages and requests between different JavaScript contexts: web workers, iframes, browser tabs, and Figma plugins. You describe the messages once as a TypeScript type, and both sides get checked against it.

* **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.** `createTypedRpcChannel` adds request/response calls on top of plain messages, with abort and timeout support.
* **Pluggable transports.** `EventTarget` and `postMessage` transports ship with the package. A custom transport is a small object with `on` and `emit` functions.
* **No runtime dependencies.** The core starts at 341 B.

```ts
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(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](https://typed-channel.psdcoder.dev/).
