当函数可能返回未定义时,TypeScript 没有报告任何错误



getStage可能返回未定义但未报告任何错误。这将使我的代码崩溃。

const a = Math.random() > 0.4
function getStage(): string {
if(a) {
return ''
}
}
const str: string = getStage()

在tsc编译之后

var a = Math.random() > 0.4;
function getStage() {
if (a) {
return '';
}
}
var str = getStage();

strictNullChecks-检查它是否在ts配置中关闭

最新更新