typescript 跳过未使用参数的类型检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38835001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip type check on unused parameters
提问by Zyphrax
When I compile my typescript project, I'm using the noImplicitAny
option so that I won't forget to specify the types on my variables and arguments.
当我编译我的打字稿项目时,我使用了这个noImplicitAny
选项,这样我就不会忘记在我的变量和参数上指定类型。
However sometimes you have arguments that you don't use. For example:
但是,有时您有不使用的参数。例如:
jQuery.ajaxTransport("+*", function (options: JQueryAjaxSettings) {
return {
abort: function (_, callback: JQueryCallback) {
I am not interested in the first argument of the abort function, so I ignore it by naming it _.
我对 abort 函数的第一个参数不感兴趣,所以我通过将它命名为 _ 来忽略它。
Is that the proper way to do that in TypeScript? I couldn't find it in the guide. I suspect that it isn't the proper way, because I can only name one argument _.
这是在 TypeScript 中做到这一点的正确方法吗?我在指南中找不到它。我怀疑这不是正确的方法,因为我只能将一个参数命名为 _。
Typescript raises the following error:
打字稿引发以下错误:
error TS7006: Parameter '_' implicitly has an 'any' type.
错误 TS7006:参数“_”隐式具有“任何”类型。
I could just type _:any
but that seems a bit overkill for an argument that I don't use.
我可以只打字,_:any
但对于我不使用的参数来说,这似乎有点矫枉过正。
回答by Keith
I was having the same problem. Using say express and routing you would often only want the res parameter.
我遇到了同样的问题。使用 say express 和 routing 你通常只需要 res 参数。
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.
这看起来更好,因为我认为只做 '_' 可能会使使用 lodash/underscore 有点混乱,而且它也很明显它是您感兴趣的第四个参数。
回答by 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:
使用_0
, _1
, ... 解决方案时,我遇到了以下问题:
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
.
正如此处所写,如果给定参数是undefined
or ,则设置默认值可避免错误抛出null
。
function parent({}={}, {}={}, {}={}) {
function child({}={}, {}={}, {}={}) {
// :-)
}
}