【问题标题】:error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary错误在条件表达式中不必要地使用布尔文字 no-unneeded-ternary
【发布时间】:2017-07-31 01:30:51
【问题描述】:

我在atomESLint 中尝试这个code

代码sn-p:

$scope.IsVisible = false;
$scope.ShowHide = function () {
  $scope.IsVisible = $scope.IsVisible ? false : true; // error
};

收到此 ESLint 错误: 错误在条件表达式中不必要地使用布尔文字 no-unneeded-ternary

尝试使用这些解决方案 solution 1solution 2,但错误未修复。此外,code 在没有 ESLint 的编辑器中也能正常工作。

【问题讨论】:

  • $scope.IsVisible = !$scope.IsVisible;

标签: javascript angularjs atom-editor ternary-operator eslint


【解决方案1】:

在我的例子中,我想根据一个对象或 null 的变量为我的布尔值分配一个值。

正如kylesimmonds 所说,可以使用双敲!! 运算符。

const myObject: SomeObjectType | null;
...

const isObjectExistent: boolean = !!myObject;
/*
  instead of:
  const isObjectExistent: boolean = myObject? true : false;
*/

检查:What is the !! (not not) operator in JavaScript?

【讨论】:

    【解决方案2】:

    试试好方法 =)。在这种情况下不需要使用语句。

    $scope.IsVisible = !$scope.IsVisible;
    

    【讨论】:

    • 如果您像我一样需要检查 true 值,请使用双键:!!$scope.IsVisible
    猜你喜欢
    • 2020-08-19
    • 2016-06-12
    • 2020-12-15
    • 2018-09-14
    • 2018-10-02
    • 2017-08-25
    • 2018-07-28
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多