0

I want 5 numbers ranging from 1 to 5 (numberOfScreen has value 5 which comes from the API captured in it) to be pushed into the array, but I am getting the below error

core.js:6241 ERROR TypeError: Cannot read property 'push' of undefined

Find the code snippet below:

screens: Array<number>;
numberOfScreen: number;

get screenNumbers(): Number[] {
  for (let i = 1; i <= this.numberOfScreen; i++) {
    this.screens.push(i);
  }
  return this.screens;
}

I am new to Typescript so I wanted to understand where I am going wrong.

1
  • screens: Array<number>; is not yet initialized. screens = []; Commented Jul 22, 2020 at 9:18

1 Answer 1

1

Initialize your screens variable with blank array;

screens: Array<number> = [];
Sign up to request clarification or add additional context in comments.

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.