DEV Community

Cover image for Meet banditypes — the smallest TypeScript validation library
Vladimir Klepov
Vladimir Klepov

Posted on

Meet banditypes — the smallest TypeScript validation library

I'm excited to announce banditypes — a new validation library for TS and JS. You may have heard of zod or superstruct — these great tools let you define a JS schema to validate objects, and automatically convert the schema to a TS type to avoid duplication. Well, banditypes does the same thing, but in 400 bytes!

A code sample of banditypes validator

Curious to learn how it can be so small? It boils down to three design principles:

  1. Strict scope cutting. More full-featured validation libraries tell you what, exactly, failed validation. This feature had to go.
  2. Export functions, not objects with methods, because functions tree-shake better. This is exactly why superstruct is already 3–5 times lighter than zod.
  3. More extensibiltiy. If you cut features from the core, the users must be able to write the missing bits themselves.

And some more optimization on top:

  1. Compile to modern JS. Gzip is good, but replacing function () with () => still saved 20 bytes!
  2. Minimal API. Bye-bye literal(42), we have enums([42])
  3. Gzip-aware code style. Gzip removes repetition, so you need to make different parts of code as similar as possible. Write Object once, repeat for free. Surprise: copy-pasting functions with minor changes is often smaller under gzip than proper code reuse.

I have published an article with a more in-depth explanation.


Banditypes is not a validation library for every use case, but it gains something from its minimal design, and was fun to build. Check it out on GitHub, and maybe even give it a spin in your next project!

Top comments (0)