ArkType Documentation
repository·main·Indexed 27 days ago
https://github.com/arktypeio/arktypeA 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.
What's inside ArkType
- 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).
Understand Type Introspection via .internal
mainArkType provides access to its internal type representation through the
.internalproperty on eachType. This representation is defined in@ark/schemaand consists of a tree of nodes. Each node has akindproperty that describes its purpose and structure.Note: APIs under
.internalare not officially frozen and are not guaranteed to be semver-stable, though they are considered stable enough for direct access to introspection capabilities.Understand the @ark/schema structure
mainThe
@ark/schemapackage 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
instanceofa specific class (implies the"object"domain). - Unit: A type that must
===a specific value.
- Domain: Built-in TS-like keywords for non-enumerable value sets (
- 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
Requiredconditions 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 (
Lis assignable toR) is defined by the property that the intersectionL & Ris equal toL.Features of the ArkType extension
mainThe extension provides two primary developer experience enhancements:
- Syntax Highlighting: Provides specialized syntax highlighting for strings that are part of an ArkType definition.
- Inline Error Summaries: Provides inline type error summaries optimized for ArkType, specifically designed to work with the ErrorLens extension.
Use ArkThemes for TypeScript highlighting
mainArkThemes 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.Upgrade to ArkType 2.0
mainArkType 2.0.0 is a stable release that provides high-performance runtime validation with TypeScript-like syntax. It is designed to be up to 100x faster than Zod at runtime and offers deep introspectability using set theory to expose relationships between types at runtime.Use built-in TypeScript keywords in ArkType
mainArkType 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
anyandvoidare 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 })Integrate ArkType with oRPC
mainoRPC has built-in support for the Standard Schema API, meaning ArkType works out of the box for defining input schemas.
import { type } from "arktype" os.input( type({ name: "string", "age?": "number" }) )Install ArkType
mainEnsure your environment meets the following requirements for ArkType:
- TypeScript: version
>=5.1. - Module System: A
package.jsonwith"type": "module"(or an environment that supports ESM imports). - tsconfig.json Configuration:
strictorstrictNullChecks(required)skipLibCheck(strongly recommended)exactOptionalPropertyTypes(recommended)
- TypeScript: version
Install ArkType extension for JetBrains IDEs
mainFor embedded syntax highlighting in JetBrains IDEs, install the ArkType plugin.Integrate ArkType with tRPC
mainArkType can be used as the input validator in tRPC. The implementation depends on your tRPC version:
- tRPC >= 11: Pass the ArkType
Typeinstance directly to.input(). - tRPC < 11: Pass the
.assertproperty of the ArkTypeTypeinstance to.input().
// tRPC >= 11 t.procedure.input( type({ name: "string", "age?": "number" }) ) // tRPC < 11 t.procedure.input( type({ name: "string", "age?": "number" }).assert )- tRPC >= 11: Pass the ArkType
Define a type with ArkType
mainUse the
typefunction fromarktypeto 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, usetypeof 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