# 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**.

```typescript
type User = {
  name: string
  age: number
}
```

If you receive JSON from an API

```typescript
const data = JSON.parse(input)
```

TypeScript **cannot guarantee** that `data` actually matches `User`.

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

```typescript
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

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

### b.Runtime validation errors

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

Invalid input throws structured errors.

### c.Full Type Inference

```typescript
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/](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

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

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

Zod

```typescript
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

```typescript
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`

```typescript
// 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](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 :)
