← Learning Hub
Advanced

Type Guards, satisfies and Checking Outside Data

Types only exist while compiling. Data from users, files and APIs arrives at runtime, so you still need to check it. A type guard is a function that checks and tells TypeScript the result.

Types disappear when code runs, so data from users and APIs can be anything. Type guards check data at runtime and tell TypeScript the result, connecting the safety of types with the reality of outside data.

Example
You can edit this code.

The in operator

Example
You can edit this code.

satisfies

satisfies checks an object against a type without losing its exact values:

Example
You can edit this code.

Exhaustive checks with never

Example
You can edit this code.
For bigger apps, a library like Zod lets you write the check once and get the type from it automatically.
Key points
  • x is T return types make type guards.
  • satisfies checks a value without widening it.
  • Use never for exhaustive checks.

📝 Test yourself

1. What does the return type x is Product mean?
2. Why check API data at runtime?
3. Fill in the keyword that checks a value against a type but keeps its exact type:
const c = { a: "#fff" } Theme;