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

0

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

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More