arktype

TypeScript type validation with ArkType, ArkEnv, and ArkRegex. Use for schemas, environment variables, and type-safe regex.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "arktype" with this command: npx skills add thegreataxios/agent-skills/thegreataxios-agent-skills-arktype

ArkType Development

ArkType is a TypeScript-first validation library with powerful runtime type checking, scopes, generics, and pattern matching. Use this skill when defining schemas, validating data, or working with type-safe APIs.

When to Apply

Reference this skill when:

  • Defining type schemas for validation
  • Creating reusable type scopes and modules
  • Working with generics and constrained types
  • Pattern matching with match
  • Validating environment variables with ArkEnv
  • Type-safe regular expressions with ArkRegex
  • Converting to/from JSON Schema
  • Testing with Attest

Quick Reference

Type Definition Syntax

SyntaxUse CaseExample
StringConcise definitionstype({ name: "string", age: "number >= 18" })
FluentChaining methodstype.string.atLeastLength(8).email()
TupleComplex/nestedtype(["string", ["number", "number"]])
ArgsOperators`type("string", "

Validation Results

MethodReturns
.validate(data)`{ data: T }
.assert(data)Validated data or throws
Type as functionValidated data or type.errors instance

Key Operators

OperatorMeaning
``
&Intersection
> / >=Greater than (exclusive/inclusive)
< / <=Less than (exclusive/inclusive)
%Divisible by
? suffixOptional property
= suffixDefault value

Core Concepts

1. Type Definition

String Syntax (Most Concise)

const User = type({
  name: "string",
  age: "number.integer >= 0",
  email: "string.email",
  "role?": "'admin' | 'user' = 'user'"
})

Fluent API (Best for chaining)

const Password = type.string
  .atLeastLength(8)
  .describe("a valid password")

2. Scopes & Modules

Define a Scope

const types = scope({
  Id: "string",
  User: { id: "Id", name: "string" },
  "User[]": "User[]"
}).export()

3. Generics

Basic Generic

const boxOf = type("<t>", { value: "t" })
const StringBox = boxOf({ type: "string" })

Constrained Generic

const nonEmpty = type("<arr extends unknown[]>", "arr > 0")

4. Pattern Matching

import { match } from "arktype"

const sizeOf = match({
  string: v => v.length,
  number: v => v,
  default: "assert"
})

// Discriminated union matching
const getValue = match
  .in<{ id: 1 } | { id: 2 }>()
  .at("id")
  .match({
    1: o => o.value,
    2: o => o.other,
    default: "assert"
  })

ArkType Ecosystem

ArkEnv (Environment Variables)

import { arkenv } from "arkenv"

const env = arkenv({
  HOST: "string.host",
  PORT: "number.port",
  NODE_ENV: "'development' | 'production' | 'test' = 'development'"
})

// Fully typed - TypeScript knows exact types!
console.log(env.HOST)     // string
console.log(env.PORT)     // number
console.log(env.NODE_ENV) // "development" | "production" | "test"

ArkEnv Keywords

KeywordValidates
string.hostHostname
string.urlURL
number.portPort (1-65535)
string.emailEmail format
string.uuid.v4UUID v4

ArkRegex (Type-safe RegExp)

import { regex } from "arkregex"

const ok = regex("^ok$", "i")
// Regex<"ok" | "oK" | "Ok" | "OK", { flags: "i" }>

const semver = regex("^(\\d*)\\.(\\d*)\\.(\\d*)$")
// Regex<`${bigint}.${bigint}.${bigint}`, { captures: [bigint, bigint, bigint] }

const email = regex("^(?<name>\\w+)@(?<domain>\\w+\\.\\w+)$")
// Regex with typed groups: { name: string; domain: `${string}.${string}` }

Features

FeatureDescription
Type inferenceInfers capture types from pattern
Named groups.groups object is fully typed
Zero runtimeUses native RegExp at runtime
TS 5.9+Required for best experience

Attest (Testing)

import { attest, setup } from "@ark/attest"

setup()

it("type tests", () => {
  attest<string>(myType.infer)
  attest(myType.json).snap({ /* ... */ })
})

JSON Schema

// Type to JSON Schema
const schema = User.toJsonSchema()

// JSON Schema to Type
import { jsonSchemaToType } from "@ark/json-schema"
const T = jsonSchemaToType({ type: "string", minLength: 5 })

Common Patterns

Recursive Types

const Node = scope({
  Node: {
    value: "string",
    "children?": "Node[]"
  }
}).export().Node

Discriminated Unions

const Event = type({
  type: "'click'",
  x: "number",
  y: "number"
}).or({
  type: "'keydown'",
  key: "string"
})

Branded Types

const Even = type("number % 2").brand("even")
type Even = typeof Even.infer

How to Work

  1. Choose syntax: String for conciseness, fluent for chaining
  2. Define schema: Use type() for one-off, scope() for reusable
  3. Validate: Call type as function or use .assert()
  4. Handle errors: Check instanceof type.errors
  5. Export modules: Use .export() for public APIs

Related Resources

  • arktype: github.com/arktypeio/arktype
  • arkregex: Type-safe RegExp replacement
  • arkenv: Environment variables with ArkType
  • @ark/attest: Testing utilities
  • @ark/json-schema: JSON Schema conversion

Related Skills

  • typescript - TypeScript best practices

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

skale-dev

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

bite-dev

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

zod

No summary provided by upstream source.

Repository SourceNeeds Review
Automation

skale-docs

No summary provided by upstream source.

Repository SourceNeeds Review