Skip type check on unused parameters

本小妞迷上赌 提交于 2019-12-03 06:13:25

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

router.get('/', 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.

Miaow

I may be late, but I got stuck with the other solutions and this one work all the time for me:

function ({}={}, {}={}, {}={}, onlyThis) { console.log(onlyThis); }

comparison

When using the _0, _1, ... solution, I faced difficulties with scooped function like:

function parent(_0, _1, _2) {
  function child(_0, _1, _2) {
    // TypeScript crying about shallowed variables
  }
}

but with the empty object it work well:

function parent({}, {}, {}) {
  function child({}, {}, {}) {
    // :-)
  }
}

Even if the parameters are typed like:

function parent({}: number, {}: string, {}: any) {
  function child({}: number, {}: string, {}: any) {
    // :-) x2
  }
}

EDIT:

And as written here, setting a default value avoid error throwing if the given parameter is undefined or null.

function parent({}={}, {}={}, {}={}) {
  function child({}={}, {}={}, {}={}) {
    // :-)
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!