ArkType

Define a type with TypeScript syntax, compose it, and validate data with it.

If you already know TypeScript, you already know most of ArkType's syntax.

Define

import { type } from "arktype"

const 
const User: Type<{
    name: string;
    platform: "android" | "ios";
    versions?: (string | number)[];
}>
User
= type({
name: "string", platform: "'android' | 'ios'", "versions?": "(number | string)[]" }) // { name: string; platform: "android" | "ios"; versions?: (number | string)[] } type
type User = {
    name: string;
    platform: "android" | "ios";
    versions?: (string | number)[];
}
User
= typeof
const User: Type<{
    name: string;
    platform: "android" | "ios";
    versions?: (string | number)[];
}>
User
.infer

A key ending in ? is optional, as in TypeScript. String literals like 'android' need their own quotes inside the definition.

Definitions are checked by TypeScript, so a mistake is an error in your editor, with autocomplete to help fix it:

const 
const User: Type<{
    name: never;
}>
User
= type({
name: "strng"
TypeScript: 'strng' is unresolvable​
})

Compose

Suppose we want to move platform and versions from our original type to a new device property.

const 
const User: Type<{
    name: string;
    device: {
        platform: "android" | "ios";
        versions?: (string | number)[];
    };
}>
User
= type({
name: "string", // nested definitions don't need to be wrapped device: { platform: "'android' | 'ios'", "versions?": "(number | string)[]" } })

To decouple device from User, move it to its own type and reference it.

const 
const Device: Type<{
    platform: "android" | "ios";
    versions?: (string | number)[];
}>
Device
= type({
platform: "'android' | 'ios'", "versions?": "(number | string)[]" }) const
const User: Type<{
    name: string;
    device: {
        platform: "android" | "ios";
        versions?: (string | number)[];
    };
}>
User
= type({
name: "string", device:
const Device: Type<{
    platform: "android" | "ios";
    versions?: (string | number)[];
}>
Device
})

Validate

Call a type with unknown data to get back either the validated data or an ArkErrors object describing every problem.

const 
const out: {
    name: string;
    device: {
        platform: "android" | "ios";
        versions?: (string | number)[];
    };
} | ArkErrors
out
= User({
name: "Alan Turing", device: { platform: "enigma", versions: [0, "1", 0n] } }) if (
const out: {
    name: string;
    device: {
        platform: "android" | "ios";
        versions?: (string | number)[];
    };
} | ArkErrors
out
instanceof type.errors) {
// ArkErrors: device.platform must be "android" or "ios" // device.versions[2] must be a number or a string console.error(const out: ArkErrorsout.summary) } else { // out is narrowed to the validated User console.log(`Hello, ${
const out: {
    name: string;
    device: {
        platform: "android" | "ios";
        versions?: (string | number)[];
    };
}
out
.name}`)
}

Calling a type never throws for invalid data. Each problem is in out.issues, with its path, code and message; see Errors.

Where invalid data is a bug rather than something to handle, use .assert. It returns the validated data or throws a TraversalError describing the same problems.

// TraversalError: name must be a string (was missing)
const 
const user: {
    name: string;
}
user
=
const User: Type<{
    name: string;
}>
User
.assert({})

You now know how to define a Type and use it to check your data at runtime.

Next, we'll look at how ArkType extends TypeScript's type system with runtime constraints like string.email or number.integer < 100.