ArkType is a runtime type validation library that uses TypeScript's own type syntax. It is the fastest validator in the ecosystem and produces 1:1 TypeScript types from your definitions.
Installation
npm install arktype
Basic Types
import { type } from "arktype";
// Define types using TS syntax
const user = type({
name: "string",
email: "string.email",
age: "number.integer >= 0",
role: "admin | user | moderator"
});
// Validate data
const result = user({ name: "Alex", email: "alex@dev.com", age: 28, role: "admin" });
if (result instanceof type.errors) {
console.log(result.summary); // Human-readable errors
} else {
console.log(result.name); // Fully typed!
}
Advanced Types
// Arrays and tuples
const tags = type("string[]");
const pair = type(["string", "number"]);
// Optional and default
const config = type({
host: "string",
port: "number = 3000",
"debug?": "boolean"
});
// Unions and intersections
const response = type({
status: "success | error",
data: "string | number | null"
});
// Morphs (transform on validate)
const stringToNumber = type("string").pipe(s => parseInt(s, 10));
const result = stringToNumber("42"); // 42 (number)
Nested Objects
const address = type({
street: "string",
city: "string",
zip: "/^\\d{5}$/"
});
const company = type({
name: "string",
address: address,
employees: "number.integer > 0",
founded: "string.date.iso"
});
Pattern Validation
// Regex patterns
const slug = type("/^[a-z0-9-]+$/");
const uuid = type("string.uuid");
const url = type("string.url");
// Numeric constraints
const percentage = type("number >= 0 <= 100");
const positiveInt = type("number.integer > 0");
Why ArkType?
- Fastest validator — benchmarks show 100x faster than Zod for complex schemas
- TS syntax — no new API to learn, just write TypeScript types
- 1:1 type inference — extracted types match your runtime validators exactly
- Tiny bundle — ~5KB gzipped
Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.
Top comments (0)