ArkType is a runtime type validation library that uses TypeScript syntax directly. Write types once, validate at runtime — with the best error messages in the ecosystem.
Why ArkType?
Unlike Zod or Yup, ArkType lets you write validators using actual TypeScript-like syntax. No method chains, no special DSL — just types.
Quick Start
npm install arktype
import { type } from 'arktype';
const user = type({
name: 'string',
email: 'string.email',
age: 'number > 0',
});
// Validate data
const result = user({ name: 'Alice', email: 'alice@example.com', age: 30 });
if (result instanceof type.errors) {
console.log(result.summary);
} else {
console.log(result); // typed as { name: string, email: string, age: number }
}
Advanced Types
// Union types
const status = type('"active" | "inactive" | "pending"');
// Array types
const tags = type('string[]');
// Tuple types
const point = type(['number', 'number', 'number']);
// Optional properties
const config = type({
host: 'string',
port: 'number = 3000', // default value
'debug?': 'boolean',
});
Morphs (Transform + Validate)
const parseDate = type('string').pipe((s) => new Date(s));
const parseInt = type('string').pipe((s, ctx) => {
const n = Number(s);
if (isNaN(n)) return ctx.error('a numeric string');
return n;
});
const formData = type({
name: 'string',
age: type('string').pipe(s => Number(s)),
joined: type('string').pipe(s => new Date(s)),
});
Scopes for Complex Schemas
import { scope } from 'arktype';
const $ = scope({
user: {
name: 'string',
email: 'string.email',
posts: 'post[]',
},
post: {
title: 'string',
body: 'string',
author: 'user',
},
});
const types = $.export();
const User = types.user;
Performance
ArkType is up to 100x faster than Zod for complex schemas. It compiles validators at definition time, not at validation time.
| Library | Simple Object | Complex Nested |
|---|---|---|
| ArkType | 1x (fastest) | 1x (fastest) |
| Zod | 10-20x slower | 50-100x slower |
| TypeBox | 2-5x slower | 5-10x slower |
Building APIs that need data validation? I create custom web scraping and data processing solutions. Check out my Apify actors or email spinov001@gmail.com for custom projects.
Are you using ArkType or still on Zod? Share your experience below!
Top comments (0)