let isDone: boolean = false;
if(isDone == true){
console.log(`isDone equals true!`);
}
if(isDone == false){
console.log(`isDone equals false!`);
}
编译器将抛出“此条件将始终返回'false',因为类型'false'和'true'没有重叠。”以上代码错误。
上述代码的解决方法是
let isDone: boolean = false;
if(Boolean(isDone) == true){
console.log(`isDone equals true!`);
}
if(Boolean(isDone) == false){
console.log(`isDone equals false!`);
}
但是在C#中很好
using System;
public class Program
{
public static void Main()
{
Boolean isDone = true;
if(isDone == true){
Console.WriteLine("isDone equals true!");
}
else if(isDone == false){
Console.WriteLine("isDone equals false!");
}
}
}
这背后的原因是什么?还是仅仅是语法问题?