I'm trying to document an Express middleware, but the build-in validation tool in WebStorm tells me that types are incorrectly assigned in the following JSDoc block:
/**
* My middleware.
*
* @param {Object} req
* @param {Object} res
* @param {Function} next
* @return {Object}
*/
exports.show = function(req, res, next) {
...
};
In Express sources, I didn't find any @typedef
s to help me. Also, I want to avoid things like @param {*}
.
What is the correct way to document Express middleware using JSDoc? Thanks for any help.
Use DefinitelyTyped
npm install --save-dev @types/express
@param {e.Response} res
More types
/node_modules/@types/express/index.d.ts
... declare namespace e { ... export interface Response extends core.Response { } ...
WebStorm
install types via Settings > Languages & Frameworks > Javascript > Libraries > @types/express
req
,res
andnext
are all objects, and a middleware usually doesn't return, so the following may be used.The only thing you need to change is the
@param {Function}
next to@param {Object}
. Also,@return
should describe what the function returns; e.g,(Object, Array)
or a combination({Object|Null})
You can not only JsDoc the parameter types and descriptions, and but also their expected members.
First, we agree that middleware are functions; no special type declaration will generally be warranted. Beyond that, middleware tend to be highly decoupled—modular—which means the
@module
tag is usually applicable (and this has nice consequences when you run jsdoc).The return tag is optional depending on your style guide, since middleware don't return a value. Finally, contrary to what Nick and mmm claim, the
next
parameter is a function.next
isn't a fancy Express-internal concoction; Express passes each middleware function the request, the response, and the next middleware function in the stack, like this:The value of
next
within the scope ofmw1
ismw2
.