TypeScript Type Validation
Compile-Time Safety Guide
TypeScript Type Validation: Compile-Time Safety
Complete guide to using TypeScript's type system to validate context and prevent errors at compile-time.
Overview
TypeScript provides several mechanisms to validate types at compile time before any code runs:
- Strict Interfaces - Define what fields MUST exist
- Union Types - Restrict values to known options
- Literal Types - Enforce exact values
- Readonly - Prevent accidental mutations
- Generics - Validate at function boundaries
- Conditional Types - Advanced type logic
Part 1: Strict Interfaces
Require All Fields
TypeScript Validation
Catch Errors at Compile Time
Part 2: Optional vs Required
Distinguish Between Optional and Required
TypeScript Catches Undefined Errors
Part 3: Type Narrowing with Guards
Create Type Guards
TypeScript Validates Type Narrowing
How It Works
Part 4: Union Types for Validation
Restrict to Known Values
TypeScript Catches Invalid Values
Use with Readonly for Constants
Part 5: Readonly for Immutability
Prevent Accidental Mutations
Part 6: Generics for Reusable Validation
Generic Type Parameters
TypeScript Validates Generic Usage
Part 7: Conditional Types (Advanced)
Validate Based on Conditions
Part 8: Real-World Validation Examples
Example 1: Validate Function Arguments
Example 2: Validate Constructor Arguments
Example 3: Validate Transport Usage
Part 9: Enable Strict TypeScript Checks
TypeScript Config (tsconfig.json)
Result: Maximum Validation
Part 10: Validation Checklist
At Compile Time (TypeScript)
- All required fields present
- No optional property access without check
- Type matches function parameters
- Union types valid values only
- Readonly fields not mutated
- Generics properly constrained
At Runtime (Optional)
- assertions for additional validation
- Guard clauses for conditional logic
- Middleware for cross-cutting validation
Part 11: Type Validation in Our Implementation
TelegramContextFull Interface ✅
AdminTelegramContext Interface ✅
Type Guard ✅
Function Validation ✅
Part 12: Error Messages
Clear Error Messages
TypeScript provides clear error messages when validation fails:
Lists Exactly What's Missing
Summary: TypeScript Validation Benefits
| Type Validation | Benefit |
|---|---|
| Strict Interfaces | Catch missing fields at compile-time |
| Union Types | Only valid values allowed |
| Optional vs Required | Prevent undefined errors |
| Type Guards | Safe type narrowing |
| Readonly | Prevent accidental mutations |
| Generics | Reusable type validation |
| Conditional Types | Complex validation logic |
Comparison: Compile-Time vs Runtime
| Approach | When | Cost | Catch Rate |
|---|---|---|---|
| TypeScript | Compile | 0 (free) | 100% for syntax |
| Assertions | Runtime | Small | 100% for logic |
| Both | Both | Small | 100% total |
Our Implementation: Both TypeScript + Runtime = Maximum Safety ✅
Quick Reference
See Also
packages/cloudflare-agent/src/context-validation.ts- Implementationdocs/context-typing-guide.md- 5 validation approachesdocs/integration-guide.md- How to use in your code