LibraryLearning
Back to course

Guardrails • Lesson 5

Tests, Types, and Guardrails

20 minute lesson

Learning objectives

  • Understand why AI coding needs guardrails
  • See where types and tests reduce damage
  • Build safer workflows

What Is It?

Tests, types, and guardrails are the mechanisms that catch bad changes automatically. In an AI-assisted workflow, they matter even more because the model can generate a lot of code very quickly, and human review alone does not scale.

Think of them as the codebase talking back.

How It Actually Works

Types constrain what values can flow through the system. Tests constrain what behaviors are allowed to change. Validation layers constrain what external input the system will accept. Together they reduce the number of silent failures that AI-generated code can introduce.

For example, TypeScript can catch a missing field or incompatible return type before runtime. Schema validators like Zod can reject malformed inputs at the boundary. Unit and integration tests can reveal when a refactor preserved compilation but changed behavior. Linters and formatters catch consistency issues that otherwise turn into review noise.

The deeper reason guardrails matter is that language models optimize for plausibility, not correctness. If the environment does not supply strong feedback, bad code can look fine. Machine-checkable guardrails turn hidden mistakes into visible failures.

The Jargon Decoded

  • Type system — Rules about what kinds of values code can use.
  • Unit test — A small test for one function or component.
  • Integration test — A test that checks several parts working together.
  • Schema validation — Checking that data matches an expected shape.
  • Invariant check — A runtime assertion that a critical condition still holds.

Why This Matters When You're Building

This is how you keep velocity without surrendering reliability. A well-instrumented codebase lets you use AI more aggressively because bad suggestions get caught earlier.

It also reduces dependence on memory. The system itself remembers what must stay true.

What To Tell The AI

  • “Implement this change with the strongest reasonable guardrails: update types, add validation at input boundaries, and write the minimum tests covering the happy path and one failure path.”
  • “Do not weaken types or bypass validation just to make the build pass.”
  • “If a type error appears, explain what contract is being violated rather than patching with any.”
  • “Suggest where a runtime assertion or monitoring check would catch this class of bug in production.”

Common Misconceptions

“Tests slow vibe coding down.” They slow reckless coding down. They speed sustainable coding up.

“TypeScript means I am safe.” Types help, but they do not prove business logic is correct.

“AI can write tests, so I do not need to think about them.” AI can help write tests, but you still need to decide what is worth protecting.

Sources

  • TypeScript Handbook
  • Zod documentation
  • Kent C. Dodds, Testing Trophy
  • Google Testing Blog

Checkpoint questions

  • What kinds of errors do types catch well?
  • Why are tests more important when code is generated faster?

Exercise

List the minimum guardrails you want on any AI-generated production change.

Memory recall

Quick quiz

Use retrieval, not rereading. Answer from memory, then check the feedback.

1. What kinds of errors do types catch well?

2. Why are tests even more important when code is generated faster?

3. What is the role of guardrails in AI coding?

Progress

Mark this lesson complete when done