ArkType Documentation

repository·main·Indexed 27 days ago

https://github.com/arktypeio/arktype

A TypeScript 1:1 validator optimized from editor to runtime. The ecosystem includes @ark/attest for runtime type assertions and performance benchmarking, @ark/json-schema for bidirectional conversion between JSON Schema and ArkType, @ark/regex for typed regular expressions, and @ark/schema for the underlying schema language. Also provides a VS Code extension for syntax highlighting and inline error summaries.

Tokens
51.9K
Snippets
140
Records
352
Agent score
91%

What's inside ArkType

  1. Overview of ArkType

    main
    ArkType is a runtime validation library designed to be a 1:1 validator for TypeScript. It parses optimized validators using familiar, type-safe syntax. It is primarily used to validate external data, such as JSON payloads or form inputs, at the boundaries of your application (similar to how Zod is used).
  2. Understand Type Introspection via .internal

    main

    ArkType provides access to its internal type representation through the .internal property on each Type. This representation is defined in @ark/schema and consists of a tree of nodes. Each node has a kind property that describes its purpose and structure.

    Note: APIs under .internal are not officially frozen and are not guaranteed to be semver-stable, though they are considered stable enough for direct access to introspection capabilities.

  3. Understand the @ark/schema structure

    main

    The @ark/schema package provides the underlying schema language parsed from ArkType syntax. It represents the parts of the ArkType type system that exist in TypeScript (excluding runtime-only constraints like bounds, divisors, custom predicates, or morphs).

    The schema is structured as follows:

    • Union: A set of intersections.
    • Intersection: A set consisting of a Basis and Constraints.
    • Basis: The base type to which refinements (like props) are applied. It can be:
      • Domain: Built-in TS-like keywords for non-enumerable value sets ("string" | "number" | "bigint" | "object" | "symbol").
      • Proto: A type that must be an instanceof a specific class (implies the "object" domain).
      • Unit: A type that must === a specific value.
    • Constraint: Individual conditions that must be satisfied:
      • Required: A specified literal string or symbol key must be present with a value conforming to a specified union or intersection.
      • Optional: The Required conditions are met, or the specified key is simply not present.
      • Index: All keys satisfying an index type must have values satisfying the corresponding value type.

    In this system, type assignability (L is assignable to R) is defined by the property that the intersection L & R is equal to L.

  4. Features of the ArkType extension

    main

    The extension provides two primary developer experience enhancements:

    1. Syntax Highlighting: Provides specialized syntax highlighting for strings that are part of an ArkType definition.
    2. Inline Error Summaries: Provides inline type error summaries optimized for ArkType, specifically designed to work with the ErrorLens extension.
  5. Use ArkThemes for TypeScript highlighting

    main
    ArkThemes is a collection of themes designed specifically for syntax highlighting, featuring specialized support for TypeScript generics. It provides both Dark and Light theme options to improve the readability of ArkType schemas and TypeScript code.
  6. Use built-in TypeScript keywords in ArkType

    main

    ArkType allows you to use built-in TypeScript keywords directly within your type definitions. You can use the string syntax or the fluent API. Note that any and void are not included as keywords by default as they are considered unnecessary for runtime validation.

    // Using string syntax
    const Keywords = type({
    	string: "string",
    	date: "Date"
    })
    
    // Using fluent API
    const Keywords = type({
    	string: type.string,
    	date: type.Date
    })
  7. Install ArkType

    main

    Ensure your environment meets the following requirements for ArkType:

    • TypeScript: version >=5.1.
    • Module System: A package.json with "type": "module" (or an environment that supports ESM imports).
    • tsconfig.json Configuration:
      • strict or strictNullChecks (required)
      • skipLibCheck (strongly recommended)
      • exactOptionalPropertyTypes (recommended)
  8. Integrate ArkType with tRPC

    main

    ArkType can be used as the input validator in tRPC. The implementation depends on your tRPC version:

    • tRPC >= 11: Pass the ArkType Type instance directly to .input().
    • tRPC < 11: Pass the .assert property of the ArkType Type instance to .input().
    // tRPC >= 11
    t.procedure.input(
    	type({
    		name: "string",
    		"age?": "number"
    	})
    )
    
    // tRPC < 11
    t.procedure.input(
    	type({
    		name: "string",
    		"age?": "number"
    	}).assert
    )
  9. Define a type with ArkType

    main

    Use the type function from arktype to define a schema using a syntax that closely resembles TypeScript. You can define objects, primitive types, unions, and optional properties (using the ? suffix). To extract the TypeScript type from an ArkType definition for use in your code, use typeof YourType.infer.

    import { type } from "arktype"
    
    const User = type({
    	name: "string",
    	platform: "'android' | 'ios'",
    	"versions?": "(number | string)[]"
    })
    
    // extract the type if needed
    type User = typeof User.infer