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

error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary

我正在尝试这个code in atom with ESLint

代码片段:

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

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

尝试使用这些解决方案 and solution 2, but error is not fixed. Furthermore, code 在没有 ESLint 的编辑器中工作正常。

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

$scope.IsVisible = !$scope.IsVisible;

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

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?