Express Middleware vs TypeScript - Best Practices?

Topics: General
Sep 16, 2013 at 5:12 AM
Edited Sep 16, 2013 at 5:13 AM
A common practice when using Express middleware is to augment the request and response objects with additional functions and variables for use by controllers and views. Given that these are applied in an adhoc fashion and therefore don't represent the primary signature of the request and response objects they are applied to, I'm curious what others are doing to work around this when accessing those augmentations in code. Casting? What else?
Sep 16, 2013 at 7:58 AM

I know what you are referring to. The best approach i have found is to simply extend the base interface http.ServerRequest / Response with your additional methods and vars. Working through the interface, you can type cast the 'object' into its appropriate type, and just add additional methods and vars on that interface as they become known. (casting is going to be unavoidable at some point)

You can also extend interfaces. So, you can just extend ExpressServerRequest with what you need..

side note, you may be interested in this project.

good luck.
Sep 16, 2013 at 8:02 AM
Yeah I was thinking of casting to interfaces that extend the base express request type, which seems along the lines of your suggestion. I don't think I'd touch the core express definitions though (purity and all).

I'll check out appex, thanks.