
ArkType Has a Free API — Here's How to Validate Data with TypeScript's Type Syntax
ArkType is a runtime type validation library that uses TypeScript's own type syntax. It is the fastest validator in the ecosystem and produces 1:1 TypeScript types from your definitions. Installation npm install arktype Basic Types import { type } from " arktype " ; // Define types using TS syntax const user = type ({ name : " string " , email : " string.email " , age : " number.integer >= 0 " , role : " admin | user | moderator " }); // Validate data const result = user ({ name : " Alex " , email : " alex@dev.com " , age : 28 , role : " admin " }); if ( result instanceof type . errors ) { console . log ( result . summary ); // Human-readable errors } else { console . log ( result . name ); // Fully typed! } Advanced Types // Arrays and tuples const tags = type ( " string[] " ); const pair = type ([ " string " , " number " ]); // Optional and default const config = type ({ host : " string " , port : " number = 3000 " , " debug? " : " boolean " }); // Unions and intersections const resp
Continue reading on Dev.to Webdev
Opens in a new tab

