Now in a class, I declared an array with type: CustomType, like below
class Example {
public exampleArray: CustomType[];
public clearArray() {
this.exampleArray = [];
}
}
As you can see the clearArray assign an UNDEFINED type of an empty array, which seems lost the type information.
How I can clear the array but preserving its declared type?
There can be 3 ways of emptying an array
setting its length = 0
myArr.length = 0;
using splice method Array
myArr.splice(0,myArr.length);
Pop() each element of array
while(myArr.length){
myArr.pop();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With