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

10

67

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;
}
Marney answered 20/7, 2018 at 15:51 Comment(1)
Not a TS user, but my guess is that lint detects a return in your function, so it considers your function always has to return something, which is not the case. What if you add a return true; after the else statement?W
S
5

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);
Sasha answered 20/7, 2018 at 15:54 Comment(3)
i am not using ES2017 so includes will be a problem, any other solution ?Marney
@Marney then .indexOf(el) > -1Sasha
Perfect Thanks alotMarney
I
103

The complaint is that the first if(){} is missing an else{} block with a return statement. You can disable this behaviour in tsconfig.json:

compilerOptions:{
  :
  "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

Incrassate answered 21/4, 2020 at 8:34 Comment(0)
M
15

tsconfig.json

compilerOptions:{
  "noImplicitReturns": false
}
Magenmagena answered 20/3, 2021 at 0:39 Comment(0)
G
10

You can resolve this error by two ways.

  1. By editing the noImplicitReturns attribute to false in tsconfig.json

    "noImplicitReturns": false

enter image description here

  1. By adding a return statement for every path in your method. If you have 10 if conditions, then you have to add 10 return statements. It seems odd, but typescript recommends return for every path.

Here we can avoid the number of paths by the use of lambda expression.

private ValidateRequestArgs(str) {
  return str.split(",").every(el => List.includes(el));
}
Gass answered 4/8, 2021 at 13:32 Comment(0)
S
5

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);
Sasha answered 20/7, 2018 at 15:54 Comment(3)
i am not using ES2017 so includes will be a problem, any other solution ?Marney
@Marney then .indexOf(el) > -1Sasha
Perfect Thanks alotMarney
A
5

You can define the returned value for you function like the following:

functionName: (variableName: string): string | void => {
    if (variableName === "a") {
        return "link";
    }
};
Adjunct answered 11/6, 2022 at 12:17 Comment(0)
M
2

If there is a return in the forEach function, then it recognizes the function as having a return type for the whole function.

There is no problem to write your loop this way:

myFunction(): boolean {
    for (let item of items) {
      if (condition) {
        return true;
      }
    }
    return false;
}
Measurable answered 4/10, 2021 at 20:33 Comment(1)
I didn't realize that return in a foreach loop just returns from the loop, not the outside function. Your method is a lot less confusingThoreau
S
1

foreach no need to return anything; you must be remove keyword 'return' from foreach and edit and the error points to this:

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) {
      ret = false // this correct
    }
    if (List.indexOf(val) > -1) {
      ret = true;
    } else {
      ret = false;
    }
  });
  return ret;
}

Sclerosed answered 25/11, 2021 at 12:21 Comment(0)
C
0

The body of the function passed to forEach has an implicit signature of any -> boolean so it would seem that tslint is expecting you to treat it more statically and return boolean on all code paths.

Choppy answered 21/7, 2018 at 18:50 Comment(0)
F
0

if you are facing this using .forEach for-loop use for-of for-loop as below,

   for (var val of str.split(',')) { }
Famished answered 30/12, 2022 at 5:29 Comment(0)
R
0

typescript forces us to return something on each path, if we have written our statements conditionally.

If you only want to return when only those conditions are met, not when they are not met, simply give your function 'any' return type:

function myFunc() : any{

}

Renowned answered 24/4, 2023 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.