Core Concepts
Type Safety
End-to-end type safety from request validation to response serialization. TypeScript automatically knows what properties are available—no guessing, no runtime errors.
Overview
Define your schema once. Get runtime validation and compile-time types. TypeScript automatically knows what's available.
Define once, get both validation and types
typescript
Catch errors at compile time, not in production. Get IntelliSense everywhere. Without type safety, you're guessing property names and hoping the API returns what you expect.
Without Types
- Guessing property names
- Runtime errors in production
- No autocomplete or IntelliSense
- Manual type checking everywhere
With MoroJS
- Automatic type inference
- Errors caught at compile time
- Full IntelliSense support
- TypeScript knows what's available
Automatic type inference from schemas
typescript
Catch Errors Early
TypeScript catches errors before your code runs. No more production surprises.
Better DX
Full autocomplete and IntelliSense. Write code faster with confidence.
Self-Documenting
Types serve as documentation. See what's available without reading docs.
How It Works
MoroJS automatically infers types from your route patterns, validation schemas, and middleware context. You define your data structure once, and TypeScript knows about it everywhere.
Request Type Safety
Route parameters, query strings, and request bodies are automatically typed based on your route patterns and schemas.
Automatic Parameter Type Inference
typescript
Query Parameter Types
typescript
Schema-Based Request Body Validation
typescript
Parameter Validation
typescript
Response Type Safety
Define response schemas to ensure your API returns consistent data. TypeScript ensures your responses match the schema.
Response Schema Definition
typescript
Context Type Safety
Middleware can add typed data to the request context. TypeScript knows what's available in your handlers.
Typed Middleware Context
typescript
Advanced Type Patterns
Branded Types for Better Safety
typescript
Conditional Response Types
typescript
Best Practices
Do
- Define schemas for all inputs and outputs
- Use strict TypeScript configuration
- Leverage z.infer for type extraction
- Create reusable schema components
- Use branded types for IDs
- Document complex types with JSDoc
Don't
- Use 'any' types
- Skip validation for external data
- Ignore TypeScript errors
- Mix validated and unvalidated data
- Use type assertions without validation
- Forget to handle all response cases