typia for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is typia and why is it needed?

\n\n

typia is a powerful library for TypeScript that provides runtime type validation with a unique feature: it generates validation code at compile-time, not at runtime. This makes it one of the fastest data validation libraries in the TypeScript ecosystem.

\n\n

Main problems that typia solves:

\n
    \n
  • Performance: Unlike popular libraries (zod, yup, io-ts), typia does not use reflection or decorators. Instead, it generates optimized validation code at build time, providing a 10–100x speed increase.
  • \n
  • Type Safety: You get a guarantee that data coming from external sources (API, databases, forms) matches your TypeScript interfaces.
  • \n
  • Minimal Boilerplate: You don't need to write separate validation schemas — typia uses your existing TypeScript types.
  • \n
  • Support for Complex Types: Union, intersection, conditional types, mapped types — all work "out of the box".
  • \n
\n\n

The library is especially useful for:

\n
    \n
  • Backend development on Node.js with TypeScript
  • \n
  • Frontend applications where form validation or API response validation is required
  • \n
  • Microservice architecture where serialization/deserialization speed is important
  • \n
  • Projects where performance and type safety are critical
  • \n
\n\n

Installation

\n\n

Installing typia consists of two steps: the library itself and the TypeScript transformer (compiler).

\n\n

Step 1. Installation via npm

\n
npm install typia\n# or\nyarn add typia\n# or\npnpm add typia
\n\n

Step 2. Installing the transformer

\n
npm install --save-dev typia-transform\n# or\nyarn add -D typia-transform\n# or\npnpm add -D typia-transform
\n\n

Step 3. Configuring tsconfig.json

\n

Add the plugin to your tsconfig.json:

\n
{\n  "compilerOptions": {\n    "plugins": [\n      {\n        "transform": "typia-transform"\n      }\n    ]\n  }\n}
\n\n

Step 4. Using with ttypescript (recommended)

\n

Install ttypescript for automatic transformation:

\n
npm install --save-dev ttypescript
\n

Then replace tsc with ttsc in your package.json scripts:

\n
{\n  "scripts": {\n    "build": "ttsc",\n    "start": "node dist/index.js"\n  }\n}
\n\n

Important: typia requires TypeScript version 4.4 or higher. The library will not work without the transformer.

\n\n

Quick Start with Code Example

\n\n

Let's create a simple example of user data validation.

\n\n
import typia from "typia";\n\n// Define the interface\ninterface IUser {\n  id: number;\n  name: string;\n  email: string;\n  age: number;\n  isActive: boolean;\n}\n\n// Create the validation function\nconst validateUser = typia.createValidate<IUser>();\n\n// Test data\nconst testData = {\n  id: 1,\n  name: "Ivan Petrov",\n  email: "ivan@example.com",\n  age: 30,\n  isActive: true,\n};\n\n// Validation\nconst result = validateUser(testData);\n\nif (result.success) {\n  console.log("Data is valid:", result.data);\n} else {\n  console.log("Validation error:", result.errors);\n}\n\n// Example with incorrect data\nconst badData = {\n  id: "not a number", // Should be number\n  name: "Ivan",\n  email: "not an email",\n  age: -5,\n  isActive: "yes", // Should be boolean\n};\n\nconst badResult = validateUser(badData);\nconsole.log("Errors:", badResult.errors);
\n\n

This example shows how easy it is to validate data using only a TypeScript interface. No additional schemas or decorators!

\n\n

Main Methods and Functions

\n\n

1. typia.

Recommendations