Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 and Typescript array - Property 'push' does not exist on type '() => void'

How do I init an empty array?

test: any[];

//On change I want to add an item to an array
test(){ 
  this.test.push('a');
}

error TS2339: Property 'push' does not exist on type '() => void'.

like image 444
Tampa Avatar asked Nov 29 '22 09:11

Tampa


1 Answers

You don't initialize your array, so test is undefined. To be able to use it, just initialize it this way:

test: any[] = [];
like image 189
Thierry Templier Avatar answered Dec 15 '22 17:12

Thierry Templier