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; }
The comlaint is that the first if(){} is missing an else{} block with a return statement. You can disable this behaviour in a tsconfig file setting:
"noImplicitReturns": false,
Of course you could also add
else {return ...}
But I would not recommend that, since forEach is not supposed to return anything as stated for example here: What does `return` keyword mean inside `forEach` function? or here: https://codeburst.io/javascript-map-vs-foreach-f38111822c0f
Instead better get rid of the first if() altogether. Cheers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With