Typia is a TypeScript validation library that transforms your TypeScript types into super-fast validation functions.
Instead of writing separate schemas like other validation libraries, Typia reads your existing TypeScript types and creates optimized validation code at compile time.
This guide will show you how to use Typia in your TypeScript projects. You'll learn how to set it up, create validation functions, handle errors, and integrate it with web frameworks.
Let's get started!
Prerequisites
Before you start with Typia, make sure you have a recent version of Node.js and npm installed. You should also know TypeScript basics since Typia only works with TypeScript projects.
Setting up the project environment
Setting up Typia is significantly easier as it includes an automated setup wizard. The wizard handles all configuration for you, so you don't need to edit configuration files or manually install dependencies.
Create a new directory and navigate into it:
Initialize your project:
Install Typia and run the automated setup:
The setup wizard automatically handles everything for you:
- Installs required dependencies (
typescript,ts-patch) - Configures your
tsconfig.jsonwith the right settings - Sets up the transformer plugin
- Adds the necessary npm scripts
That's it! Typia can now transform your TypeScript types into optimized validation functions during code compilation.
Understanding Typia's fundamentals
Typia works differently from other validation libraries. Instead of defining schemas, you just use regular TypeScript interfaces and types. Typia reads these types when you compile your code and generates super-fast validation functions.
Create a validation.ts file in your project:
This code defines a simple User interface and creates two validation functions:
validateUser: Returns detailed results with error information if validation failscheckUser: Returnstrueorfalseto tell you if the data is valid
Now create an index.ts file to test these validators:
The checkUser function gives you a simple yes/no answer. The validateUser function gives you detailed information. When the data matches your User interface, validateUser returns a success object with the validated data. When validation fails, it returns specific error information showing exactly what went wrong.
Add the build script to your package.json:
Compile and run the application:
With valid input data, you'll see:
Here's what makes Typia helpful: when you compile your code, Typia transforms your validation functions into highly optimized JavaScript. The typia.createIs<User>() call becomes a specific function that directly checks for string and number types without any runtime overhead.
Validation with special tags
Typia lets you add validation rules that go beyond basic type checking. You do this using special tags that work with TypeScript's intersection types. These tags let you express complex validation logic while keeping your code type-safe.
Adding validation constraints
You can add specific validation rules using Typia's built-in tags. Update your validation.ts file:
Each tag adds a specific rule:
tags.MinLength<3>andtags.MaxLength<50>: The name must be between 3 and 50 characterstags.Type<"uint32">: The age must be a positive integer within 32-bit rangetags.ExclusiveMinimum<18>: The age must be greater than 18 (not equal to 18)tags.Format<"email">: The email must be in valid email formattags.Pattern<"...">: The password must match the regex pattern (requires numbers, lowercase, and uppercase letters)
Test these rules with invalid data in your index.ts:
With the invalid data in place, run the script:
Running this code shows you specific error messages:
Typia's error reporting tells you exactly which fields failed and why, making it easy to fix problems or show helpful error messages to users.
When you need validation rules that aren't built into Typia, you can create custom tags using the tags.TagBase interface:
Custom tags work by defining JavaScript code in the validate property. Typia injects this code into the generated validation function. The $input variable represents the value being checked, and your validation code should return true for valid values or false for invalid ones.
Combining multiple validation rules
Typia does well at combining multiple validation rules into comprehensive type definitions. You can create complex validation schemas by chaining tags together:
This schema shows how Typia handles nested objects, arrays with item constraints, union types, and complex validation combinations. The validation function checks every constraint efficiently while providing detailed error information when validation fails.
Integrating Typia with Express
Typia fits naturally into backend frameworks like Express, where validating request bodies is a common task. You can use Typia to validate incoming data before your route handlers process it, ensuring type safety from the API boundary all the way through your application.
Start by installing Express and its TypeScript types:
Let's use our existing User interface from the validation file. Make sure your validation.ts looks like this:
Now create a basic server in a new file called server.ts:
In this example, Typia validates the incoming JSON against the User interface. If the data is invalid, the API responds with clear error messages. If validation passes, it proceeds with the validated data.
Build and run the server:
You should see:
Now test the API with valid data using curl (in a new terminal):
Or use Postman:
You'll get a success response:
Now test with invalid data to see how Typia handles validation errors:
Or in Postman:
You'll get a validation error response:
The server console will show:
Typia makes it clear where the data doesn’t match the expected type, helping you catch issues early and respond with helpful errors.
Final thoughts
Typia helps you validate data using your existing TypeScript types. It keeps your code simple, boosts performance by generating optimized validation functions at build time, and provides detailed error messages when something doesn’t match.
You can learn more in the Typia documentation or explore the source on GitHub.