DEV Community

Peter Leonov
Peter Leonov

Posted on

TypeScript type predicate generator

Hey folks! I've been working recently on several rather high load TypeScript projects that would consume a bunch of JSON API responses. No surprises I need to verify that the response data meets the definition in TypeScript to catch API regressions earlier. There are several solutions out there, but I want something simple and efficient specifically to fit the rising demand for high performance JS on mobile devices and in the various server runtimes.

Following the schema based code generation path other languages are using I've come up with a simple but effective type to code generator that takes a TS type and outputs a small readable function in strict TS that ensures a given unknown value satisfies the type. The produced code is JIT friendly and minifies really well.

As it is just a bunch of TS functions with no compiler magic involved any bundler, dev tool or TS aware runtime (like Deno, Bun and recently Node) can use it as is. Just import { isUser } from './type_predicates' and you're good to go.

Check out the playground and the repo. You can use it as a CLI tool or simply copy the generated code from the playground. Let me know what you think!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

đź‘‹ Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay