如何解决不是所有代码路径都返回值的问题? [英] How to fix the issue not all the code paths return value?
本文介绍了如何解决不是所有代码路径都返回值的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试解决的代码中有错误.我认为它需要一个 return
语句,但我已经在 forEach
循环之外,但它仍然抛出错误:
I have an error in the code that I am trying to resolve. I think it needs a return
statement but I have that already outside of the forEach
loop but it still throws the error:
not all the code path return the value
如何修复以下代码?
main.ts
:
private ValidateRequestArgs(str) {
let ret: boolean = true;
// here on val its throwing tslint error not all code paths return value
str.split(',').forEach((val) => {
if (!ret) {
return false;
}
if (List.indexOf(val) > -1) {
ret = true;
} else {
ret = false;
}
});
return ret;
}
推荐答案
我不知道为什么 tslint 抱怨,但你可以把整个事情写得更优雅:
I'm not sure why tslint is complaining, but you can write the whole thing way more elegant as:
return str.split(",").every(el => List.includes(el));
或 ES6:
return str.split(",").every(el => List.indexOf(el) > -1);
这篇关于如何解决不是所有代码路径都返回值的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文