Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the issue not all the code paths return value?

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; } 
like image 348
hussain Avatar asked Jul 20 '18 15:07

hussain


1 Answers

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

like image 171
theRealEmu Avatar answered Sep 20 '22 07:09

theRealEmu