hussain
hussain

Reputation: 7113

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

Upvotes: 74

Views: 145323

Answers (10)

navinrangar
navinrangar

Reputation: 1388

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{

}

Upvotes: 1

theRealEmu
theRealEmu

Reputation: 1504

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

Upvotes: 110

Alish Giri
Alish Giri

Reputation: 2238

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

   for (var val of str.split(',')) { }

Upvotes: 0

Alaa M
Alaa M

Reputation: 427

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

functionName: (variableName: string): string | void => {
    if (variableName === "a") {
        return "link";
    }
};

Upvotes: 5

Hossein
Hossein

Reputation: 230

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

Upvotes: 1

A. Morel
A. Morel

Reputation: 10374

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

Upvotes: 2

Codemaker2015
Codemaker2015

Reputation: 15599

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

Upvotes: 10

rohit.khurmi095
rohit.khurmi095

Reputation: 2661

tsconfig.json

compilerOptions:{
  "noImplicitReturns": false
}

Upvotes: 16

ChaosPandion
ChaosPandion

Reputation: 78282

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.

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138447

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

Upvotes: 5

Related Questions