DEV Community

Eliav2
Eliav2

Posted on

Real end-to-end type-safety with Expressjs

TLDR: Check out my new end-to-end type adapter for Express.js: express-typed


express-typed-gif

I’ve always appreciated the simplicity of Express, but it was created back in 2009, long before TypeScript emerged in 2014. Consequently, type safety wasn’t built into the framework from the start.

While alternatives like trpc exist, they often come bundled with peculiar adapters specific to trpc, not always seamlessly fitting into other setups.

Another option ts-rest and zodios, but they require upfront type definitions imported into both backend and frontend. This approach can be tedious, especially when dealing with dynamic backend types, such as those in Prisma ORM.

When confronted with these challenges in my recent project, I decided to build a custom type adapter for Express.js Router. This solution relies heavily on type declarations, wrapped in a tiny runtime wrapper around Express.Router, that enables type inference (as TypeScript inference primarily operates declaratively: during assignment and return, while Express.Router routes are declared imperatively)

This library was successfully deployed in production with my latest project, and the results were so impressive that I couldn’t resist extracting the type adapter into a separate library to share with you all.

Check out the GitHub repo for more details, and please share any suggestions or issues you encounter!

Top comments (0)