---
title: "How to make postMessage type-safe in under 350 bytes"
description: "typed-channel wraps postMessage, BroadcastChannel, MessagePort and EventTarget behind one TypeScript-checked emit/on API, where message names and payloads are declared once in shared type maps and mirrored on each side."
url: "https://swe.quest/articles/how-to-make-postmessage-type-safe-in-under-350-bytes-64613f692ee55f2d"
canonical: "https://evilmartians.com/chronicles/how-to-make-postmessage-type-safe-in-under-350-bytes"
source: "Evil Martians"
published: "2026-10-28"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["typescript","browsers","frontend","open-source"]
---

# How to make postMessage type-safe in under 350 bytes

A post on [Evil Martians](https://swe.quest/sources/evil-martians.md), published 2026-10-28, 10 min read, How-to. Original: [https://evilmartians.com/chronicles/how-to-make-postmessage-type-safe-in-under-350-bytes](https://evilmartians.com/chronicles/how-to-make-postmessage-type-safe-in-under-350-bytes)

## Summary

typed-channel wraps postMessage, BroadcastChannel, MessagePort and EventTarget behind one TypeScript-checked emit/on API, where message names and payloads are declared once in shared type maps and mirrored on each side. Transports are pluggable objects with just on and emit, so custom boundaries like Figma plugin sandboxes take about a dozen lines, and one channel can fan out over several transports. Version 0.11 adds createTypedRpcChannel with typed request/handle calls whose AbortSignal propagates to the remote handler, rejects on thrown errors, and cleans up pending requests on unlisten. The core is 271 bytes brotli-compressed (341 with the postMessage transport), while the RPC factory is a separate 1 kB import.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [TypeScript](https://swe.quest/tags/typescript.md) (Languages)
- [Browsers](https://swe.quest/tags/browsers.md) (Web & mobile)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)
- [Open source](https://swe.quest/tags/open-source.md) (Engineering practices)

## Prerequisites

- TypeScript generics ([easy posts on TypeScript](https://swe.quest/tags/typescript.md?difficulty=easy))
- postMessage and Web Workers
- AbortController

## Keywords

typed-channel, postMessage, Web Workers, BroadcastChannel, AbortSignal, Figma plugins, Harmonizer, MessageChannel

## Related posts

- [500x faster postMessage(string)](https://swe.quest/articles/500x-faster-postmessage-string-575e19a5ee69829f.md): Bun · 2025-08-20 · Medium · 3 min read · Runtimes & VMs, JavaScript, Performance engineering, Concurrency
  Bun v1.2.21 makes postMessage(string) between workers nearly independent of string size, up to 500x faster with ~22x less peak memory on 3 MB strings (593 ns vs 326,290 ns in 1.2.20).
- [Bun v0.7.0](https://swe.quest/articles/bun-v0-7-0-d3096f46f4bdb50f.md): Bun · 2023-07-21 · Medium · 10 min read · JavaScript, Runtimes & VMs, Concurrency
  Bun v0.7.0 focuses on Node.js compatibility: it can now run \`vite dev\` (experimentally, via \`bun --bun\` to override the node shebang), supports Web \`Worker\` threads with \`postMessage\` and \`structuredClone()\`, and implements \`AsyncLocalStorage\` from \`node:async\_hooks\` as a step toward Next.js support.
- [Bun v0.7.2](https://swe.quest/articles/bun-v0-7-2-65b9975794c342cb.md): Bun · 2023-08-03 · Medium · 4 min read · JavaScript, Runtimes & VMs, Memory management
  Bun v0.7.2 adds node:worker\_threads, node:diagnostics\_channel and a global BroadcastChannel, lets structuredClone/postMessage clone Error objects, and adds \`bun .\` to run a project's main file.
- [September in Servo: \<progress\>, rounded rectangles, and more!](https://swe.quest/articles/september-in-servo-progress-rounded-rectangles-and-more-366001ae43187fe1.md): Servo · 2026-11-05 · Easy · 8 min read · Browsers, Rust, Performance engineering, Memory management
  Servo 0.7.0 bundles 527 commits from September, adding web platform features such as the \<progress\> element, :checked, user-select, CSS typed arithmetic, alpha(), font-width, Canvas roundRect() and an MLKEM768-X25519 WebCrypto algorithm, plus many new DOM APIs.
- [What's coming to JavaScript](https://swe.quest/articles/what-s-coming-to-javascript-bea63ec11e3bd18c.md): Deno · 2025-06-26 · Medium · 6 min read · JavaScript, Language design
  TC39's 108th meeting advanced nine JavaScript proposals. Explicit Resource Management (\`using\`/\`await using\` with \`Symbol.dispose\`), \`Array.fromAsync\` and \`Error.isError\` reached stage 4, and Immutable \`ArrayBuffer\` (\`transferToImmutable\`, \`sliceToImmutable\`) reached stage 3.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
