Issue
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
How to fix below code?
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;
}
Solution
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));
or ES6:
return str.split(",").every(el => List.indexOf(el) > -1);
Answered By - Jonas Wilms
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.