当函数test
返回一个类型为any的值时,为什么在使用tsc
并打开--noImplicitAny
的情况下,以下代码在编译期间没有引发任何错误?
我不认为在这种情况下,编译器能够推断函数返回值的类型,因为依赖于If Else,返回类型是不同的.
function test(str: string)
{
if(str === "b")
{
return "abcd";
}
else
{
return 5;
}
}
console.log(typeof(test("b"));
console.log(typeof(test("c"));