Skip to main content

Command Palette

Search for a command to run...

ArkType

Published
3 min read
ArkType

ArkType is a TypeScript-first runtime type validation library. It allows you to define types and validation rules using TypeScript-like syntax, and then validate data at runtime while keeping full TypeScript type inference.

In simple terms

ArkType lets you define a type once, and use it for both compile-time typing and runtime validation

1.Why ArkType is Needed

In TypeScript, types exist only during development (compile time). When your code runs, TypeScript types disappear.

type User = {
  name: string
  age: number
}

If you receive JSON from an API

const data = JSON.parse(input)

TypeScript cannot guarantee that data actually matches User.

This is where ArkType helps — it validates the data at runtime.

import { type } from "arktype"

const User = type({
  name: "string",
  age: "number"
})

const result = User({ name: "John", age: 30 })

if (result instanceof Error) {
  console.log(result.message)
} else {
  console.log("Valid user")
}

What happens in the above code

  1. We define a schema

  2. Pass data to it

  3. ArkType validates it at runtime

  4. TypeScript automatically knows the type.

2.Features of ArkType

a.Typescript like syntax

const User = type({
  id: "number",
  email: "string.email",
  age: "number >= 18"
})

b.Runtime validation errors

User({
  id: 1,
  email: "test@mail.com",
  age: 20
})

Invalid input throws structured errors.

c.Full Type Inference

type UserType = typeof User.infer

TypeScript automatically understands the type.

d. Very Fast

ArkType is designed to be extremely fast compared to many validation libraries like Zod, Yup. ArkType takes 14 nanoseconds at runtime which is 20x faster than Zod and 2000x faster than Yup.

You can check the benchmarks here: https://moltar.github.io/typescript-runtime-type-benchmarks/

3.Comparison with other libraries

Library Syntax Speed TypeScript Integration
ArkType Very Simple Very Fast Excellent
Zod Verbose Medium Good
Yup Verbose Slower Limited
Joi Complex Medium Weak

ArkType

const User = type({
  name: "string",
  age: "number > 18"
})

// extract the type if needed
type User = typeof User.infer

Zod

const User = z.object({
  name: z.string(),
  age: z.number().min(18)
})

ArkType is much shorter and closer to natural TypeScript.

4.Installation & Setup of ArkType in your Project

You can install arktype with the following command

bun install arktype

Ensure you have

  • TypeScript version >=5.1.

  • A package.json with "type": "module" (or an environment that supports ESM imports)

  • A tsconfig.json with...

    • strict or strictNullChecks (required)

    • skipLibCheck (recommended)

    • exactOptionalPropertyTypes (recommended)

VS Code Extension - To take advantage of all ArkType's autocomplete capabilities, you'll need to add the following to your workspace settings at ./vscode/settings.json

// allow autocomplete for ArkType expressions like "string | num"
"editor.quickSuggestions": {
	"strings": "on"
},
// prioritize ArkType's "type" for autoimports
"typescript.preferences.autoImportSpecifierExcludeRegexes": [
	"^(node:)?os$"
],

You can check more about ArkType from their official documentation: https://arktype.io/docs/intro/setup

Well you have gathered some information about ArkType today and just start building things with ArkType and see how it works for you.

Will catchup in another interesting post :)

More from this blog

Nidhin's blog

168 posts

✨Crafting Code with a Smile for 8 Years:) Merging the Formal Dance of Angular, the Playful Rhythms of React, and the Next-level Moves of Next.js 🚀