3

whenever i used forEach() i got this error in terminal

[ts] Property 'forEach' does not exist on type 'string'.

My application works in browser properly. Everything works properly but i don't want such errors in terminal.

I used below code for it. Please suggest me what i missing.

 job.forEach(function(state) {
        if((totalcarer['userId']+"")===state.rNo)
        {
          cjobState = state.cJobstatus;
        }
      });
11
  • 1
    Incomplete information. You need provide all the relevant information to the question and bug. Commented Mar 6, 2017 at 5:58
  • 1
    Its not an array. your job is a string That is what error is trying to say. Commented Mar 6, 2017 at 5:59
  • 2
    If the error message says it's a string it probably is a string. Commented Mar 6, 2017 at 5:59
  • 1
    @harishmahajan You cant provide one by one information. It gets too dragy. Can you provide everything inorder for us to re-create the bug? Commented Mar 6, 2017 at 6:05
  • 1
    @harishmahajan var job=jobs['sentCarer'] how can you say job is an array. it might be a string also. Commented Mar 6, 2017 at 6:06

2 Answers 2

10
arrays: string[];
arrayString: string;

See the diff between? one is array and another is string.

arrays.forEach(...) // valid.
arrayString.forEach(...) //invalid.
Sign up to request clarification or add additional context in comments.

3 Comments

What is jobs ?
it is an array variable. thanks. now i used public job:String; and works
Then what does ['sentCarer'] do?
1

The issue is that TypeScript is trying to save you from accidentially calling forEach on keys when it's a string type, as it could fail on runtime. You will need to make sure keys are type of array before calling foreach:

if (Array.isArray(job)) {
  job.forEach(...);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.