ArkType: TypeScript you can take to runtime(github.com) |
ArkType: TypeScript you can take to runtime(github.com) |
By far the most common question I get is "why use this over existing validators like Zod?" so I'll preemptively address it:
- There's a comparison between Zod and ArkType for defining a simple type under "Concision" on our docs site (https://arktype.io). The definition is much shorter and the inference is cleaner and more intuitive. This is not a cherry-picked case. The examples are from Zod's documentation and ArkType's syntax and type hints are highly optimized for clarity.
- Zod recently announced they were deprecating discriminated unions because it was hard to maintain given their architecture. Not only are they supported in arktype, they're supported implicitly- you don't even have to tell us what keys to look at.
- The discriminated unions point is really just a special case of a much more general advantage: arktype is not just a validator- it's a type system. If we can deeply intersect arbitrary types, optimize them, and determine assignability, we can use all of that to make discrimination much easier (being able to "discriminate" is equivalent to having some common path that has an intersection of never)
- Most ArkType definitions are just objects and strings and can be easily serialized and eventually could even be used to validate data across multiple languages. Validators that rely on chaining will never be able to do this.
- Zod claims the following in their docs:
"You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. Instead you'll need to define the type definition manually, and provide it to Zod as a 'type hint'."
They go on to mention:
"passing cyclical data into Zod will cause an infinite loop"
ArkType can directly infer cyclic and recursive types and validate cyclic data.
The point of this is not to say Zod is not a good library. It significantly improved inference capabilities and DX relative to the previous generation of validators like Yup. It also still has some features we haven't yet implemented (primarily stuff like pick and merge as I'm working on adding support for generics). Plus, we're still on 1.0.8-alpha, so if I needed a validator for my critical production software, I'd probably go with Zod. That said, I do think arktype has some fundamental advantages that mean in the long term, it's a better approach to invest in.