Ferrotype

Types Worth Keeping

Ferrotype derives TypeScript types from Rust types, and they're the kind you'd be happy to have written by hand: tagged unions, template literal IDs, namespaces, and cross-references the Rust compiler checks.

5 minute read Source

Ferrotype is a Rust derive. Put #[derive(TS)] on the types your server sends and receives, and it writes the TypeScript types for your client, so the two can't drift apart.

There are crates that do this already. Their output compiles, but it uses hardly any of what makes TypeScript good, and you wouldn't want to have written it by hand. The bar I had in mind was a types package in ace, written by an engineer who contributes to Svelte's core: discriminated unions with a rich type tag, template literal IDs like `vm-${string}`, intersections like Claude.Todo & { createdAt: UNIX }, nested names like VM.Git.State, and indexed access to keep one type's field in step with another's. Ferrotype writes types like those.

What you get

Here's an enum from the test suite and the TypeScript it renders, exactly as the test asserts it:

#[derive(TS)]
enum Message {
    Ping,
    Text(String),
    Binary(Vec<u8>),
    Error { code: i32, message: String },
}
{ type: "Ping" } | { type: "Text"; value: string } | { type: "Binary"; value: number[] } | { type: "Error"; code: number; message: string }

Every variant carries its tag, so TypeScript narrows on type in a switch without a cast. An enum of unit variants becomes a union of strings, like "Pending" | "Active" | "Completed". Option<T> becomes T | null, Vec<T> an array, HashMap a Record, i128 and u128 become bigint, and Result<T, E> becomes { ok: true; value: T } | { ok: false; error: E }.

That's what made it worth using in agent-foundry, a Rust agent of mine with a Svelte UI. Nineteen request, response and event types derive TS, and the streaming events arrive in the UI as one union it can switch on:

export type PromptEvent = { type: "TextDelta"; text: string } | { type: "ThinkingDelta"; text: string } | { type: "ToolStart"; id: string; name: string } | { type: "ToolInput"; delta: string } | { type: "ToolResult"; id: string; content: string; is_error: boolean } | { type: "Complete"; session: ApiSession } | { type: "Error"; message: string };
Fig. 1. Rust in, a tree in the middle, TypeScript out. Nothing is a string until render(). The real output is one line, wrapped here at ; and |.

The good parts of TypeScript

The attributes follow serde's names, so anyone who knows serde can read them: rename, rename_all, skip, flatten, transparent, and for enums tag, content and untagged. There are two kinds of optional, because TypeScript has two: a plain Option<T> is field: T | null, and #[ts(optional)] makes it field?: T.

Beyond serde, the attributes reach for the features ace's types use. A template literal pattern turns a string into a checked shape:

#[derive(TS)]
#[ts(pattern = "vm-${string}")]
struct VmId(String);
type VmId = `vm-${string}`;

TypeScript will now reject "abc123" where a VmId belongs. #[ts(namespace = "VM::Git")] puts a type inside nested namespace blocks and references it as VM.Git.GitState. #[ts(extends = "Claude.Todo")] makes an intersection with a type that lives only on the TypeScript side. #[ts(wrapper = "Prettify")] wraps a definition in the helper that's in almost every TypeScript codebase, and the generator can emit Prettify itself so the file stands alone.

Fig. 2. How a derived type reaches the file: added at link time, or by hand.

Mistakes that don't compile

Indexed access goes furthest. #[ts(index = Profile, key = login)] renders a field as Profile["login"], so it follows whatever type login has. Rust has no indexed types, but a derive macro can still check one: it writes a small function that reads login from a &Profile, and if the field isn't there, the build fails (Fig. 3):

error[E0609]: no field `nonexistent` on type `&Profile`
  --> tests/compile_fail/indexed_access_invalid_field.rs:13:33
   |
13 |     #[ts(index = Profile, key = nonexistent)]
   |                                 ^^^^^^^^^^^ unknown field
   |
   = note: available fields are: `login`, `email`

That's a broken TypeScript reference caught by cargo build, before any TypeScript exists. With a string argument instead, index = "ExternalType", it can point at types Rust can't see, and skips the check.

Fig. 3. A TypeScript mistake, caught by the Rust compiler.

Using it

Add ferro-type, derive TS, and generate the file from build.rs or a flag on your binary with ferro-type-gen. Every derived type registers itself, so there's no list to keep:

let registry = TypeRegistry::from_distributed();

The generator adds export, can write one file per Rust module, and skips the write when nothing changed, which keeps build.rs from triggering rebuilds. agent-foundry writes ui/src/generated/types.ts behind a --gen-types flag and also serves the same types over HTTP.

Fig. 4. Two readers of the same struct, and only one of them saw serde's attribute.

How it works

The foundation is one trait, modeled on serde's Serialize:

pub trait TS {
    fn typescript() -> TypeDef;
}

TypeDef describes a TypeScript type as a tree: primitives, arrays, tuples, objects, unions, intersections, records, literals, template literals, indexed access, references, and named types that carry a namespace and the Rust module they came from (Fig. 1). Nothing becomes a string until it's rendered, so a registry can collect every named type once, however many places refer to it, and write one file.

Registration uses linkme: the derive adds each type to a distributed slice that the linker assembles at build time (Fig. 2). It costs nothing at runtime and doesn't depend on code running before main, which is why it won over inventory.

Fig. 5. Built in three days, then left alone. One dot per commit.

Caveats

  • It reads #[ts], not #[serde]. The attributes share serde's names, but the derive only looks at its own. In agent-foundry, SessionDetail has #[serde(flatten)] on its info field, so the JSON puts the session's fields at the top level, while the generated type says { info: ApiSession; messages: ApiMessage[] } (Fig. 4). Nothing caught it because the UI only reads messages. Every attribute has to be written twice, and a forgotten one is a type that lies. The fix is to read serde's attributes directly.
  • Generics aren't generic. Paginated<User> renders as a plain Paginated with User[] inside, not type Paginated<T>, and registering Paginated<Post> too drops one of them. TypeDef can describe a generic definition, but you have to build that tree by hand.
  • Auto-registration is easy to skip. agent-foundry ended up listing its 14 types by hand, twice. Nothing stops you from doing that, and then nothing reminds you to add the fifteenth.
  • Output order and layout. Types come out in registration order, one per line. One-file-per-module output doesn't write imports between the files yet.
  • It's paused. It was built in three days by a swarm of agents in Gas Town and has been at 0.2.0 since January (Fig. 5). The tests pass on today's stable Rust, and CI runs rustfmt, clippy and the tests, but nobody's adding to it.

The repo also has an unpublished crate that goes the other way, ferro-type-import, which parses TypeScript with swc and writes Rust with serde derives.

The name is an old one. A ferrotype is another name for a tintype, a photograph made on a thin sheet of iron: ferro for iron, which is to say Rust, and type for types. The point is the same as the bar it started from. Generated types should be ones you'd keep.