Build the safeguards
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialTypeScript / React / Runtime boundaries
A price becomes “not a number.” Two identifiers trade places. An old response overwrites a new one. Make these bugs happen, then follow the decisions that stop them.
TypeScript checks the program before it runs. A runtime validator checks the data that actually arrives. This lab uses both, then protects the workflow and asynchronous results separately.
The React application runs real compiled TypeScript against three invented sample responses. It makes no customer API calls. The public controls choose reviewed examples; they do not execute visitor-supplied code.
Your independent testing environment
Choose “A price sent as text.” Run it without safeguards, then with safeguards. Watch an invalid price become a clear validation message. Reset starts over and stops pending work.
Loading the example controls… The written lesson and code download are available below.
Show the malformed input first. Explain why the assertion compiled, where validation belongs, how the union changes the interface, and why cancellation and response ordering are separate concerns.
Use the code viewer to inspect the original source that produced this release. Each file is checked against the release manifest before it is displayed. The complete download includes the runnable example, tests, setup instructions and third-party notices.
Keep exploring
Eight explained steps, tradeoffs, pitfalls and a checkpoint before moving on.
Read the tutorialChoose a pattern and an environment for the problem you actually have.
Compare approachesA beginner route, a local setup and an extension challenge.
Open the how-to guideNo. Start with the default example, read the trace and compare the two implementations. The tutorial introduces the code after you have seen the result.
No. Examples use invented records and simulated downstream work. Each lab keeps its own transient environment; resetting it stops the experiment.
The React application runs real compiled TypeScript against three invented sample responses. It makes no customer API calls. The public controls choose reviewed examples; they do not execute visitor-supplied code.
The original lesson and application source are MIT-licensed. Third-party runtimes retain their own licenses. Download the package and follow GETTING_STARTED before changing it.
Lab discussion / TypeScript Boundary Lab
Ask about the code, compare an approach, or describe an edge case. Reader comments are reviewed before publication.
Code walkthrough
These short excerpts connect the lab’s behavior to its implementation. Each example links to the full tutorial step for its context, tradeoffs and checkpoint. Names and dependencies belong to that source; the excerpts are not standalone programs.
Read parseQuote in the code viewer. It first checks object shape, then identifier formats, then safe whole-number cents. It returns a discriminated success-or-error result. It also constructs a new object, so unrelated incoming properties do not silently become domain state. Validation is a business boundary: zero is allowed here, a negative price is not.
export type Result<T> =
| { ok: true; value: T }
| { ok: false; error: string };
Both IDs are strings at runtime, but their meanings differ. Brand<string, Name> separates CustomerId and QuoteId in checked code. The brand is introduced only after the validator checks the prefix. The negative type tests deliberately try assigning one ID type to the other. They use @ts-expect-error so a later change that removes the distinction fails type checking.
type Brand<T, Name extends string> =
T & { readonly __brand: Name };
type CustomerId = Brand<string, "CustomerId">;
Read State and transition. An approved state contains a verified quote and an approver. Idle does not carry those fields. The approve transition still checks the current state and a nonempty approver because callers can request an inappropriate action at runtime. A tagged union makes the data contract precise; the transition function enforces workflow rules.
type State =
| { status: "idle" }
| { status: "ready"; quote: Quote }
| { status: "approved"; quote: Quote; approvedBy: string };
Try competing responses. The older request deliberately takes longer. The broken path assigns whichever response finishes last. The guarded path gives each request a generation and accepts only the latest one. Completion order is not user intent. The trace tells you when a late result was ignored rather than pretending it never happened.
const mine = ++latest;
await waitForResponse();
if (guarded && mine !== latest) return;
Download the matching source and run type checking plus the five behavioral tests. Extend the validator with a currency field and define a rejected state with a reason. Write the expected failure first, implement the smallest change, then explain why the runtime check and the state change belong in different functions.
node --test tests/typescript.test.mjs
# From the development source package:
pnpm run typecheck