Superfast runtime validators with only one line
Typia is a TypeScript transformer that converts native TypeScript type definitions into highly optimized runtime validation functions through Ahead-of-Time (AOT) compilation. Unlike schema-based validation libraries that interpret type definitions at runtime, typia analyzes your TypeScript interfaces and types during compilation to generate dedicated, inline validation code tailored specifically to each type. This eliminates the overhead of runtime schema traversal and produces validation functions that execute type checks directly.
The library addresses a fundamental inefficiency in the TypeScript ecosystem: the need to maintain separate type definitions, validation schemas, and runtime decorators for the same data structures. With typia, you write standard TypeScript types once, and the compiler generates everything else automatically. The transformation happens via ts-patch, which intercepts TypeScript compilation to inject optimized validation logic wherever typia functions are called.
Typia supports complex type scenarios including discriminated unions, template literals, branded types, and validation constraints expressed through JSDoc tags like @minimum, @maxLength, and @format email. The library has gained adoption in backend APIs where validation performance is critical, particularly in high-throughput Node.js services. With over 238,000 weekly downloads, it's become a viable alternative to traditional runtime validators when compile-time transformation is acceptable in your build pipeline.
The library extends beyond validation to include JSON serialization (claiming 200x performance improvements), Protocol Buffers encoding without .proto files, JSON Schema generation, and LLM function calling schema composition. These features share the same AOT philosophy: analyze types at compile time, generate optimized runtime code, avoid reflection and runtime interpretation.
import typia from 'typia';
// Define types with validation constraints via JSDoc
interface CreateUserRequest {
/** @format email */
email: string;
/** @minLength 3 @maxLength 50 */
username: string;
/** @minimum 18 @maximum 120 */
age: number;
/** @format uuid */
organizationId?: string;
role: 'admin' | 'user' | 'guest';
}
// Compile-time transformed validation functions
export const validateUser = typia.createValidate<CreateUserRequest>();
export const assertUser = (input: unknown): CreateUserRequest =>
typia.assert<CreateUserRequest>(input);
// Express route example
import express from 'express';
const app = express();
app.post('/users', (req, res) => {
const validation = validateUser(req.body);
if (!validation.success) {
return res.status(400).json({
errors: validation.errors.map(e => ({
path: e.path,
expected: e.expected,
value: e.value
}))
});
}
const user = validation.data;
// user is now fully typed as CreateUserRequest
console.log(`Creating user ${user.username} with role ${user.role}`);
res.json({ id: 'generated-id', ...user });
});
// Fast JSON serialization
interface ApiResponse {
users: CreateUserRequest[];
total: number;
}
const response: ApiResponse = {
users: [/* ... */],
total: 150
};
const json = typia.json.stringify<ApiResponse>(response);
// 200x faster than JSON.stringify for complex objectsAPI Request Validation: Express or Fastify endpoints can validate incoming request bodies with type safety and minimal runtime overhead. Using typia.assert
High-Performance JSON APIs: Services that serialize large datasets to JSON benefit from typia.json.stringify
Type-Safe Configuration Loading: Validating environment variables or config files against TypeScript interfaces ensures configuration type safety at application startup. Using typia.assert
OpenAI/Claude Function Calling: The typia.llm.application
Protocol Buffers Without .proto Files: Microservices using protobuf can encode/decode messages using native TypeScript types with typia.protobuf.encode
npm install typiapnpm add typiabun add typia