DEV Community

Cover image for My Type of Coding: Express Route Param Types
Ryan Kahn
Ryan Kahn

Posted on

My Type of Coding: Express Route Param Types

How does express extract the route parameters in TypeScript, such that we can read them off the request.params object in a typesafe way? It's quite ingenious, and uses template literal types, conditional types, and the infer keyword to recursively process the parameter names out of the path string and apply required and optional parameters to the params object in route handlers. Here are those types extracted into a CodeSandbox. If you open it on CodeSandbox you can see the intellisense at work!

Top comments (0)