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.
You can document your middleware with
const express = require("express"); /** * @param {express.Request} req * @param {express.Response} res * @param {express.NextFunction} next */ function (req, res, next) {}
when you have middleware that add attribute to req, you can also add them with
const express = require("express"); /** * @param {express.Request & {specialParam1 : string, specialParam2 : any}} req * @param {express.Response} res * @param {express.NextFunction} next */ function (req, res, next) {}
or event better, create a typedef for each source of new elem added on "req" and use "&" to create a type combining them all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With