zod icon indicating copy to clipboard operation
zod copied to clipboard

Comparison with typebox?

Open kevinlul opened this issue 2 years ago • 4 comments

The README has a great breakdown of how Zod compares with many similar libraries: https://github.com/colinhacks/zod#comparison. @sinclair/typebox could be added to the comparison; it seems to be comparable with similar syntax, and is widely used.

kevinlul avatar Jun 04 '23 13:06 kevinlul

+1 Seems like a good idea.

zgayjjf avatar Jul 22 '23 10:07 zgayjjf

November, 2022 from the author of typebox on reddit:

TypeBox does however support the following additional functionality (if you need it)

  • Strict compliance with the JSON Schema specification.
  • Currently fastest JIT compiler / validator for JavaScript.
  • Closer alignment to the TypeScript Type System and Utility Types.
  • Supports Recursive Type Inference
  • Supports Conditional Type Mapping
  • Supports Custom Types with User Defined Inference Rules.
  • Value API for performing type operations on JavaScript values.
  • Extensive Test Suite for Ajv compliance
  • A more low level API to allow you to tune validation pipelines.

The benefit of using TypeBox mostly comes from being aligned to an industry standard specification (JSON Schema). From this you can leverage many frameworks that standardize on the specification (Fastify, OpenAPI, etc), utilize tools to auto generate documentation, generate user interface forms, or simply share types (and validation logic) across multiple systems (useful for micro services). TypeBox also uses the JSON Schema specification as it's Type Reflection Model.

In terms of performance, TypeBox currently ranks 3rd overall benchmarks here, with 1st and 2nd place given to AOT solutions (ones that require TS compiler transforms). TypeBox (like Ajv) provides a JIT compiler solution (for servers) as well as a dynamic type checking solution for applications under code evaluation constraints (for example browsers and certain cloud function providers)

Overall, I think most users of TypeBox just enjoy being closely aligned to JSON Schema specification while still being able to work with a Zod-like API.

Hope that helps

(Not sure what else might have changed since then.)

ghost avatar Jun 26 '24 17:06 ghost

My new team just asked "Why not TypeBox?" So +1 to having a comparison in the official docs. 😊

(Update): I'll add my own comparison notes as I learn more about TypeBox. (Admittedly as someone who prefers Zod)

👍 Zod:

  • Terser syntax.
  • Schema objects have a .parse(value). In TypeBox you must import and use Value.Parse(schema, value)
  • Zod supports discriminated unions, TypeBox does not.
  • Zod's z.coerce.date() is quite nice for working with dates in JSON. In TypeBox you have to write your own Transformer.

👎 TypeBox:

  • Seems oddly focused on being compatible with JSON Schema (does not support discriminated unions because it's not in JSON Schema, see link above) but then does support things which are not in JSON Schema. (ex: Type.Transform)
  • In my short time (days) with TypeBox, I've already seen coworkers misusing its API because of weak TypeScript types. (ex: SchemaOptions accepts [prop: string]: any. 🤦‍♂)

NfNitLoop avatar Mar 14 '25 17:03 NfNitLoop

Can you .refine or .superRefine on typebox? e.g. for async server-validation in client schemas?

codepunkt avatar Mar 18 '25 10:03 codepunkt

Hi, @kevinlul. I'm Dosu, and I'm helping the Zod team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You proposed adding a comparison between Zod and Typebox in the README.
  • The suggestion received positive feedback from users, who shared their own comparison notes.
  • Key points include Zod's terser syntax and support for discriminated unions, and Typebox's JSON Schema compatibility.
  • A Reddit post from the Typebox author was referenced, highlighting its features and benefits.
  • Users expressed curiosity about specific functionalities like .refine or .superRefine in Typebox.

Next Steps:

  • Please let me know if this issue is still relevant to the latest version of the Zod repository. If so, you can keep the discussion open by commenting on the issue.
  • Otherwise, the issue will be automatically closed in 28 days.

Thank you for your understanding and contribution!

dosubot[bot] avatar Jul 26 '25 16:07 dosubot[bot]

I believe this comparison is still relevant, but it doesn't seem to be relevant for the maintainers because the original section of the README I referenced is no longer present, and no part of the current Zod 4 docs discuss similar libraries.

kevinlul avatar Jul 27 '25 23:07 kevinlul