Skip type check on unused parameters

I was having the same problem.
Using say express and routing you would often only want the res parameter.

router.get("https://stackoverflow.com/", function (req, res) { res.end('Bye.'); });

Your idea of using _ works here, but I’ve also found doing this works too.

function (_1, _2, _3, onlyThis) { console.log(onlyThis); }

This seems better, as only doing ‘_’ I think might make using lodash/underscore a bit confusing, and it also makes it obvious it’s the 4th parameter your interested in.

Update: It’s been a long time since I posted this answer, and in the
comments I’m getting a few miss conceptions. So though I would
clarify.

Using the underscore trick is still very useful in Typescript. Like I mentioned in my original answer say if you was using express and did app.get("https://stackoverflow.com/", (req, res) => { you will get a warning 'req' is declared but its value is never read, but if you do -> app.get("https://stackoverflow.com/", (_req, res) => { the warning will go away. You should not get the error TS7006: Parameter 'req' implicitly has an 'any' type. error anyway, as @types/express should be implicitly typing this param anyway.

Update 2,. Please note the second answer here using {} for parameters, might look cool, but it’s considerable slower, so personally I would be careful using inside tight loops.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)