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...
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.
"string >= 8" instead of .string().min(8)lazy() wrapperstype("string") for primitivestype({ name: "string" }) for objectsscope({ ... }) for interconnected typesimport { 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
}
// Infer TypeScript type from ArkType definition
const userSchema = type({ name: "string", age: "number" })
type User = typeof userSchema.infer
// User = { name: string; age: number }
Paginated<T>).match()@ark/attest// ArkType detects discriminants automatically
const response = type([
{ status: "'success'", data: "string" },
"|",
{ status: "'error'", message: "string" }
])
// Email from specific domain
const staffEmail = type("string.email & /.*@company.com/")
// Even numbers under 100
const evenUnder100 = type("number % 2 & < 100")
// 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())
| 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) |