Typescript不应允许条件中没有括号的函数

Typescript不应允许条件中没有括号的函数,typescript,Typescript,以下代码应引发编译错误。99.9%的情况下,这不是你想要的行为。 有没有办法让typescript为此抛出编译错误 function test() { return false; } if (test) {//what you meant to do was test(), TS should report an error here console.log("This should be stopped by typescript"); } 现在,编译将以失败告终 无法将类型

以下代码应引发编译错误。99.9%的情况下,这不是你想要的行为。 有没有办法让typescript为此抛出编译错误

function test() {
    return false;
}
if (test) {//what you meant to do was test(), TS should report an error here
    console.log("This should be stopped by typescript");
}
现在,编译将以失败告终

无法将类型“()=>void”转换为类型“Boolean”


我不敢苟同。检查函数是否已定义是很常见的。还引发了-的重复。TS团队建议使用定制的线绳rule@Rob:谢谢,您知道这样的lint规则是否存在吗?也可以这样做:if(test==true),但程序员仍然必须将
显式编写为
表达式。在这种情况下,它应该读为
if(测试为函数&&test())
。如果程序员忘记显式地写这个,那仍然是一个错误陷阱。
if (test as Boolean)