javascripttypescripttslint

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;
}

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);