简体   繁体   English

使用“&&”运算符避免 ESLint 中的 no-unused-expession 警告

[英]Avoid no-unused-expession warning in ESLint using "&&"-operator

If I do the following in TypeScript, ESLint complains about the "no-unused-expession" rule:如果我在 TypeScript 中执行以下操作,ESLint 会抱怨“no-unused-expession”规则:

!hasSelectedValues && this.loadOptions([]);

How can I make ESLint understand that this expression (hasSelectedValues) is not "unused"?我怎样才能让 ESLint 明白这个表达式 (hasSelectedValues) 不是“未使用的”? I do not want to remove the rule for unused expressions completely.不想完全删除未使用表达式的规则。

To avoid running into that ESLint error , don't use && for side effects, use an if :为了避免遇到 ESLint 错误,不要使用&&作为副作用,使用if

if (!hasSelectedValues) {
    this.loadOptions([]);
}

or if you really want to keep it terse:或者如果你真的想保持简洁:

if (!hasSelectedValues) this.loadOptions([]);

How can I make ESLint understand that this expression (hasSelectedValues) is not "unused"?我怎样才能让 ESLint 明白这个表达式 (hasSelectedValues) 不是“未使用的”?

It's not complaining about hasSelectedValues , it's complaining about the result of the && operator, which your code isn't using.它不是在抱怨hasSelectedValues ,而是在抱怨您的代码未使用的&&运算符的结果。 .hasSelectedValues && this;loadOptions([]); is an expression that has a result ( false or the return value of this.loadOptions([]) ).是一个有结果的表达式( falsethis.loadOptions([])的返回值)。 That's the unusued value from the expression.那是表达式中未使用的值。

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

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