DEV Community

Alex Spinov
Alex Spinov

Posted on

ArkType Has a Free API — TypeScript Validation That's 100x Faster Than Zod

TL;DR

ArkType is a TypeScript validation library that uses type syntax you already know. It's 100x faster than Zod at runtime and provides the best type inference in the ecosystem.

What Is ArkType?

ArkType brings TypeScript types to runtime:

  • TypeScript syntax — write validators using familiar type notation
  • 100x faster than Zod — optimized compilation
  • Best-in-class inference — types inferred from your definitions
  • Morphs — transform data during validation
  • Composable — combine types with |, &, and generics
  • Free — MIT license

Quick Start

npm install arktype
Enter fullscreen mode Exit fullscreen mode
import { type } from "arktype";

// Define a type using TypeScript-like syntax
const user = type({
  name: "string",
  email: "string.email",
  age: "number.integer >= 0",
  role: "'admin' | 'user' | 'moderator'",
});

// Validate data
const result = user({ name: "Alice", email: "alice@example.com", age: 30, role: "admin" });

if (result instanceof type.errors) {
  console.log(result.summary); // Human-readable error
} else {
  console.log(result); // Validated data with correct types
}
Enter fullscreen mode Exit fullscreen mode

Type Syntax

import { type } from "arktype";

// Primitives
const str = type("string");
const num = type("number");
const bool = type("boolean");

// Constrained types
const positiveInt = type("number.integer > 0");
const email = type("string.email");
const url = type("string.url");
const uuid = type("string.uuid");

// Arrays
const strings = type("string[]");
const numbers = type("number[] >= 1"); // non-empty array

// Unions and intersections (like TypeScript!)
const stringOrNumber = type("string | number");
const nameAndAge = type({ name: "string" }).and({ age: "number" });

// Tuples
const pair = type(["string", "number"]);

// Optional properties
const config = type({
  host: "string",
  "port?": "number",
  "ssl?": "boolean",
});
Enter fullscreen mode Exit fullscreen mode

Complex Objects

const createUserInput = type({
  name: "1 < string < 100",        // string between 1-100 chars
  email: "string.email",
  age: "number.integer >= 13",
  bio: "string <= 500 | undefined", // optional, max 500 chars
  tags: "string[] <= 10",           // max 10 tags
  address: {
    street: "string",
    city: "string",
    zip: "/^\\d{5}$/",             // regex validation
    country: "'US' | 'CA' | 'UK'",
  },
});

// TypeScript type is automatically inferred!
type CreateUserInput = typeof createUserInput.infer;
Enter fullscreen mode Exit fullscreen mode

Morphs (Transform During Validation)

const parseDate = type("string").pipe((s) => new Date(s));
const parsedDate = parseDate("2024-01-15"); // Date object

const toLowerCase = type("string").pipe((s) => s.toLowerCase());
const trimAndLower = type("string")
  .pipe((s) => s.trim())
  .pipe((s) => s.toLowerCase());

const parseJson = type("string").pipe((s) => JSON.parse(s));
Enter fullscreen mode Exit fullscreen mode

Speed Comparison

Library Operations/sec Relative
ArkType 10,000,000 100x
TypeBox 8,000,000 80x
Zod 100,000 1x
Yup 50,000 0.5x
io-ts 200,000 2x

ArkType vs Zod

Feature ArkType Zod
Syntax TypeScript-like Method chaining
Speed 100x faster Baseline
Bundle size ~5 KB ~14 KB
Type inference Native Generated
Morphs/transforms Built-in .transform()
Error messages Excellent Good
Ecosystem Growing Large
Maturity Newer Established

Resources


Validating scraped web data? My Apify tools extract structured data — validate it with ArkType for type-safe, blazing-fast processing. Questions? Email spinov001@gmail.com

Top comments (0)