End-to-end type safety
Derive the client's types from the server's routes so renaming a field breaks the build, not the page. Hono RPC with Zod, and where the guarantee stops.
Shared types and code between client and serverDesigning endpoints for the UI
End-to-end type safety
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Without a shared source of truth, the client keeps its own hand-written copy of the response type, and nothing fails until a user hits the mismatch.
- Hono's RPC client infers request and response types from the server's route definitions, so the client imports only a type, never server code.
- The type check runs at compile time: it catches a client sending status blocked, but it does not validate what actually arrives over the network.
- A route's inferred response type is only as precise as the handler's return value, so a database layer that returns any produces any fields on the client.
- Error responses produced by an onError handler are not part of the inferred types, so the client still has to narrow on res.ok.
The copy that drifts
Every full-stack bug of this shape starts the same way. The server returns { project: { name } }. Someone writes a matching type in the client by hand:
type Board = { project: { id: number; title: string } };
It says title. The server says name. TypeScript is perfectly happy, because the client’s type is a promise nobody checked. The page renders a blank heading, and you find out in production or in a screenshot someone sends you.
The core problem is two sources of truth. The fix is one source, and the other side derives from it. You already met the idea in shared types and code between client and server: in a monorepo the client can import the server’s types directly. This lesson makes that concrete for the HTTP boundary.
Infer the client from the server
In Taskboard the API is a Hono app. The last line of app.ts exports its type:
export type AppType = typeof app;
The client builds its caller from that type and nothing else:
import { hc } from "hono/client";
import type { AppType } from "./app.ts";
export const api = hc<AppType>("http://localhost:8787");
import type is erased when the code is compiled, so the browser bundle gets the little fetch helper from hono/client and none of your server code. What it does get is a typed object whose shape mirrors your routes: api.api.projects[":id"].board.$get(...).
Break it on purpose
I wrote a file that does two wrong things and ran the TypeScript compiler on it (TypeScript 7.0.2, Hono 4.13.20, @hono/zod-validator 0.9.1):
await api.api.projects[":id"].tasks.$post({
param: { id: "1" },
json: { title: "x", status: "blocked" },
});
const ok = await api.api.projects[":id"].board.$get({ param: { id: "1" } });
if (ok.ok) { const b = await ok.json(); b.project.title; }
The compiler reported:
Type '"blocked"' is not assignable to type '"doing" | "done" | "todo" | undefined'.
Property 'title' does not exist on type '{ id: any; name: any; }'.
Both mistakes are caught before anything runs. The first one is interesting: the allowed status values came from the Zod schema on the server’s route (z.enum(["todo", "doing", "done"])), flowed through the validator middleware into the route’s type, and reached the client. Rename a field on the server and the client stops compiling. That is the entire payoff.
Where the guarantee stops
Read the second error carefully: { id: any; name: any }. The any is real. My handler read a row with db.prepare(...).get(), which returns any, so the inferred response type is only as exact as what the handler returns. Type safety does not flow through a hole. Use a typed query layer (the typed queries with an ORM lesson) or map rows to an explicit type at the edge of the handler.
Three more limits are worth knowing now:
- Types are erased. At runtime the browser receives whatever the network gives it. If the deployed server is one version behind the client, no type check will save you. Validate untrusted input at the server regardless; validating untrusted data at runtime is the TypeScript view of this.
- Error responses are outside the types. My
onErrorhandler produces{"error":{...}}bodies. Those do not appear in the route’s inferred type, so the client must narrow onres.okbefore calling.json(). The next lessons build the error shape on purpose. - Both sides must be checked together. If the client lives in a separate repo, there is nothing to import. The monorepo layout from the foundations group is what makes this cheap.
Other ways to get the same effect
Hono RPC is one option. Alternatives sit on a spectrum of coupling:
- tRPC gives procedures instead of URLs. It is excellent when you control both ends and no one else will call the API. It is less natural if third parties or other languages need a normal HTTP interface.
- OpenAPI plus a generator. The server publishes a schema, a tool generates a client. More ceremony, but language-independent, and it fits public APIs. See OpenAPI and API documentation tooling.
- GraphQL codegen, if you chose GraphQL.
The test is simple: can I rename a response field and have the compiler point at every place that breaks? If yes, you have the property.
One rule for Taskboard
Taskboard is one team with one client, so it uses Hono RPC. Keep one habit regardless of tool: the request schema (Zod) and the inferred type are defined once, on the server side of the repo, and imported by the client, never retyped.
What the video covers
Code with Jorge’s “Hono RPC Tutorial: Type-Safe API Client for React (End-to-End)” is about 29 minutes, published September 2025. Its timestamps cover implementing Hono RPC, building Hono types, exporting types from the API to the UI in a monorepo, a type-safe Hono client, and adding validation with zValidator and drizzle-zod. It uses React and a book-management example rather than a task board. I could not read a transcript, so I am relying on the description and timestamps. Matt Pocock’s five-minute tRPC overview is linked as a second view of the same idea with a different tool.
Where this fits
Types prove the shape compiles. They do not prove the data a user typed is acceptable. Next: validating on the client and the server.
Check yourself
What does the client import from the server in a Hono RPC setup?
Only the type of the app, written as import type { AppType }, which is erased at build time and carries no server code into the browser bundle.
If the server's board response drops a field, when do you find out?
At compile time, because the client's use of that field no longer type-checks, as long as the client and server are checked together in the same project or monorepo.
Does end-to-end type safety replace runtime validation?
No. Types are erased before the code runs, so the server still validates incoming data with something like Zod, and a client talking to an outdated server can still receive data of an unexpected shape.
Why did the board response's project id show up as any on the client?
The handler read the row from a query that returns any, and the inferred type can only be as exact as what the handler returns.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.