A short screencast building a typed fetch helper one generic at a time. TypeScript is at its best when you barely notice it. The goal of this post is fewer annotations, not more, while catching the bugs that actually reach production.
Sample footage: Sintel, an open movie by the Blender Foundation, released under a Creative Commons licence.
Let inference do the work
Inference covers most local code. Annotate function parameters and public return types, and let the compiler fill in everything else. If you find yourself writing as, stop and ask which check is missing.
- Annotate parameters and exported return types.
- Validate external data once, at the edge.
- Prefer discriminated unions over optional flags.
- Treat every as cast as a code smell to review.
Data that crosses a boundary, whether a network response, a form, or a query string, is untrusted until something validates it at runtime. Types alone cannot do that job, but they can make sure validation happens once.
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
We will revisit this in six months with fresh numbers. If the results change, we will update the post and note what changed at the end.
If you try this on your own project, start with one page or one service rather than the whole codebase. A small experiment gives you real numbers to argue with, and it is easy to roll back if the idea does not fit.
We have been running this approach for several months now. The initial setup took about a day, and the ongoing cost is close to zero once the team knows where to look.
Program testing can be used to show the presence of bugs, but never to show their absence!
Edsger W. Dijkstra, Notes on Structured Programming, 1970
This is exactly the write-up I needed this week. We hit the same wall last sprint.
This finally clicked for me after reading the docs three times.
Agreed. Start simple and only reach for this when you feel the pain.
Safari 16.4 and newer are fine in our testing; older versions need the fallback line.
We run this in production at a fairly large scale and can confirm the caveats are real.
It should work without shell access, you just lose the command-line shortcuts.
Any thoughts on how this interacts with server-side rendering?