简体   繁体   English

静默 JS 中未使用的变量警告

[英]Silencing unused variable warning in JS

My typescript compiler keeps complaining about unused variables.我的打字稿编译器一直抱怨未使用的变量。 In C, I would use在 C 中,我会使用

void foo(int bar)
{
    (void)bar;
}

Is there a JavaScript equivalent?是否有 JavaScript 等价物?

in the TSConfig file you can set "no-unused-variable": true that will make it stop complain about all the unused variables in the entire project在 TSConfig 文件中,您可以设置"no-unused-variable": true这将使它停止抱怨整个项目中所有未使用的变量

You can also put an underscore before the variable name if you want TS to ignore only that specific unused variable like this:如果您希望 TS 仅忽略该特定未使用的变量,您也可以在变量名称前放置下划线,如下所示:

function foo(_bar:number) {
  return 0;
}

A somewhat ugly, but fully functional way, is to simply use the variable for something.一种有点丑陋但功能齐全的方法是简单地将变量用于某事。

Don't know much about Typescript, but this silences warnings in C:不太了解打字稿,但这会使 C 中的警告静音:

void foo()
{
    int dummy=42;
    int x=dummy;
    dummy = x;
}

I guess you could use something similar.我想你可以使用类似的东西。

Or as Luke_ pointed out in comments, you can give the variable a name that starts with _或者正如 Luke_ 在评论中指出的那样,您可以给变量一个以_开头的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM