ArkType
ArkType is a runtime validation library for TypeScript that uses a syntax-first approach—definitions look exactly like TypeScript code. Unlike builder-pattern libraries (Zod, Yup), ArkType JIT-compiles schemas into optimized validators, achieving 10x-100x performance improvements.
Core Principles
- Write TypeScript syntax, not builder chains -
"string >= 8" instead of .string().min(8)
- Zero drift between static and runtime types - Automatic type inference from definitions
- JIT compilation for performance - Schemas compile to optimized JavaScript functions
- Native recursion support - Use Scopes for circular references without
lazy() wrappers
- Transform during validation - Morphs allow data transformation in the validation pipeline
- Standard Schema compliant - Works with React Hook Form, TanStack Query, tRPC, and other modern libraries
- Type-level performance matters - Track both runtime speed AND TypeScript compiler instantiations
- String definitions for simple types - Use
type("string") for primitives
- Object definitions for structures - Use
type({ name: "string" }) for objects
- Scopes for complex models - Use
scope({ ... }) for interconnected types
Quick Reference
Basic Validation
import { type } from "arktype"
// Simple types
const email = type("string.email")
const age = type("number >= 18")
const tags = type("string[]")
// Object schemas
const user = type({
name: "string",
age: "number >= 18",
"email?": "string.email", // Optional field
tags: "string[]"
})
// Validation
const { data, errors } = user({
name: "Alice",
age: 25,
tags: ["typescript"]
})
if (errors) {
console.error(errors.summary)
} else {
console.log(data.name) // Fully typed
}
Type Inference
// Infer TypeScript type from ArkType definition
const userSchema = type({ name: "string", age: "number" })
type User = typeof userSchema.infer
// User = { name: string; age: number }
Topics
Core Concepts
- Scopes and Recursion - Circular references and interconnected types
- Generics - Type functions for reusable schemas (e.g.,
Paginated<T>)
- Morphs - Transform data during validation (string to Date, trimming, etc.)
- Pattern Matching - Type-safe switch statements with
.match()
Advanced Features
Performance and Migration
Common Patterns
Discriminated Unions (Automatic)
// ArkType detects discriminants automatically
const response = type([
{ status: "'success'", data: "string" },
"|",
{ status: "'error'", message: "string" }
])
Constraints with Intersection
// Email from specific domain
const staffEmail = type("string.email & /.*@company.com/")
// Even numbers under 100
const evenUnder100 = type("number % 2 & < 100")
Data Transformation
// Convert string to Date during validation
const dateSchema = type("string").morph((s) => new Date(s))
// Sanitize user input
const username = type("string > 0").morph(s => s.trim().toLowerCase())
ArkType vs Zod
| Feature |
ArkType |
Zod |
| Syntax |
"string >= 5" |
z.string().min(5) |
| Performance |
JIT-compiled (10x-100x faster) |
Interpreted |
| Recursion |
Native via Scopes |
Requires z.lazy() |
| Inference |
typeof schema.infer |
z.infer<typeof schema> |
| Bundle Size |
~40kB (zero deps) |
~13kB (zero deps) |
When to Use ArkType
- High-throughput APIs - Performance critical validation (10k+ items)
- Complex domain models - Recursive data structures (folder trees, graphs)
- TypeScript-first teams - Prefer type syntax over builder patterns
- Standard Schema adoption - Need library compatibility
- Type-level optimization - Avoid "excessively deep instantiation" errors
Resources
1---2name: arktype3description: Expert knowledge for runtime validation in TypeScript using ArkType, a syntax-first validation library with TypeScript-like definitions, JIT compilation for 10x-100x performance over Zod, native recursion support, morphs for data transformation, and Standard Schema compatibility4---56# ArkType78ArkType is a runtime validation library for TypeScript that uses a **syntax-first approach**—definitions look exactly like TypeScript code. Unlike builder-pattern libraries (Zod, Yup), ArkType JIT-compiles schemas into optimized validators, achieving 10x-100x performance improvements.910## Core Principles1112- **Write TypeScript syntax, not builder chains** - `"string >= 8"` instead of `.string().min(8)`13- **Zero drift between static and runtime types** - Automatic type inference from definitions14- **JIT compilation for performance** - Schemas compile to optimized JavaScript functions15- **Native recursion support** - Use Scopes for circular references without `lazy()` wrappers16- **Transform during validation** - Morphs allow data transformation in the validation pipeline17- **Standard Schema compliant** - Works with React Hook Form, TanStack Query, tRPC, and other modern libraries18- **Type-level performance matters** - Track both runtime speed AND TypeScript compiler instantiations19- **String definitions for simple types** - Use `type("string")` for primitives20- **Object definitions for structures** - Use `type({ name: "string" })` for objects21- **Scopes for complex models** - Use `scope({ ... })` for interconnected types2223## Quick Reference2425### Basic Validation2627```typescript28import { type } from "arktype"2930// Simple types31const email = type("string.email")32const age = type("number >= 18")33const tags = type("string[]")3435// Object schemas36const user = type({37 name: "string",38 age: "number >= 18",39 "email?": "string.email", // Optional field40 tags: "string[]"41})4243// Validation44const { data, errors } = user({45 name: "Alice",46 age: 25,47 tags: ["typescript"]48})4950if (errors) {51 console.error(errors.summary)52} else {53 console.log(data.name) // Fully typed54}55```5657### Type Inference5859```typescript60// Infer TypeScript type from ArkType definition61const userSchema = type({ name: "string", age: "number" })62type User = typeof userSchema.infer63// User = { name: string; age: number }64```6566## Topics6768### Core Concepts6970- [Scopes and Recursion](./scopes-recursion.md) - Circular references and interconnected types71- [Generics](./generics.md) - Type functions for reusable schemas (e.g., `Paginated<T>`)72- [Morphs](./morphs.md) - Transform data during validation (string to Date, trimming, etc.)73- [Pattern Matching](./pattern-matching.md) - Type-safe switch statements with `.match()`7475### Advanced Features7677- [Constraints and Intersections](./constraints.md) - Ranges, regex, divisors, and type combinations78- [Custom Error Messages](./error-messages.md) - Configure errors at global, scope, or type level79- [Framework Integration](./framework-integration.md) - Hono, Fastify, and Standard Schema usage8081### Performance and Migration8283- [Benchmarking](./benchmarking.md) - Runtime and type-level performance testing with `@ark/attest`84- [Migrating from Zod](./zod-migration.md) - Syntax comparison and migration patterns8586## Common Patterns8788### Discriminated Unions (Automatic)8990```typescript91// ArkType detects discriminants automatically92const response = type([93 { status: "'success'", data: "string" },94 "|",95 { status: "'error'", message: "string" }96])97```9899### Constraints with Intersection100101```typescript102// Email from specific domain103const staffEmail = type("string.email & /.*@company.com/")104105// Even numbers under 100106const evenUnder100 = type("number % 2 & < 100")107```108109### Data Transformation110111```typescript112// Convert string to Date during validation113const dateSchema = type("string").morph((s) => new Date(s))114115// Sanitize user input116const username = type("string > 0").morph(s => s.trim().toLowerCase())117```118119## ArkType vs Zod120121| Feature | ArkType | Zod |122|---------|---------|-----|123| **Syntax** | `"string >= 5"` | `z.string().min(5)` |124| **Performance** | JIT-compiled (10x-100x faster) | Interpreted |125| **Recursion** | Native via Scopes | Requires `z.lazy()` |126| **Inference** | `typeof schema.infer` | `z.infer<typeof schema>` |127| **Bundle Size** | ~40kB (zero deps) | ~13kB (zero deps) |128129## When to Use ArkType130131- **High-throughput APIs** - Performance critical validation (10k+ items)132- **Complex domain models** - Recursive data structures (folder trees, graphs)133- **TypeScript-first teams** - Prefer type syntax over builder patterns134- **Standard Schema adoption** - Need library compatibility135- **Type-level optimization** - Avoid "excessively deep instantiation" errors136137## Resources138139- [Official Docs](https://arktype.io)140- [GitHub](https://github.com/arktypeio/arktype)141- [Benchmarks](https://moltar.github.io/typescript-runtime-type-benchmarks)142- [Standard Schema](https://github.com/standard-schema/standard-schema)