TSLint报错:expected an assignment or function call 问题求助
排查TSLint错误:
expected an assignment or function call 咱们直接定位问题核心——你代码里的这一行触发了TSLint的规则检查:
brName.indexOf(this.commonService.getBoroughs(lstItem.borough)[0].value) === -1 ? brName.push(this.commonService.getBoroughs(lstItem.borough)[0].value) : '';
错误原因
TSLint(包括后续主流的ESLint)会标记无意义的表达式语句。三元运算符a ? b : c本质是个表达式,它的核心作用是返回一个值,而非当作条件执行语句来使用。你这里把三元运算符单独作为一行语句,且else分支返回的空字符串''完全没被利用,属于无效的表达式语句,因此触发了expected an assignment or function call错误——TSLint认为你要么该把表达式结果赋值给变量,要么该调用有实际作用的函数,而非写一段无意义的表达式。
顺带提一句:你两次重复调用this.commonService.getBoroughs(lstItem.borough)[0].value,其实可以抽成变量复用,既提升性能也让代码更整洁。
解决方法
把这个三元运算符改成普通的if条件判断即可,你的逻辑是“满足条件就执行push操作”,不需要返回值,用if更符合语句的使用场景,也能顺利通过TSLint检查:
修改后的代码片段:
if (lstChildItem.cashDrawer == lstCshItems) { if (parseFloat(lstChildItem.currentBalance) != 0) { // 抽取重复取值为变量,提升可读性与性能 const boroughValue = this.commonService.getBoroughs(lstItem.borough)[0].value; // 用if替代三元运算符,符合语句使用规范 if (brName.indexOf(boroughValue) === -1) { brName.push(boroughValue); } trace[0].currBlnc.push(lstChildItem.currentBalance); currBalance = currBalance + parseFloat(lstChildItem.currentBalance); trace[0].frmName.push(this.formatCur(lstChildItem.currentBalance)); trace[0].frmCurrBlnc.push(this.formatValue(lstChildItem.currentBalance)); } }
这样修改后,不仅解决了TSLint的错误,代码的可读性也有明显提升。
内容的提问来源于stack exchange,提问作者bmovva
相关产品推荐
相关产品推荐

