14

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?

Vincent
  • 1,178
  • 1
  • 12
  • 25
  • Would `exampleArray.length = 0` do? It works in JS, not sure if TS accepts it. – VLAZ Sep 28 '18 at 10:59
  • The type information isn't lost here in the first place. What's your actual problem? This sounds like an XY question. – Ingo Bürk Sep 28 '18 at 11:00
  • Possible duplicate of [How do I empty an array in JavaScript?](https://stackoverflow.com/questions/1232040/how-do-i-empty-an-array-in-javascript) – LW001 Sep 28 '18 at 11:00
  • 2
    @LW001 not really a duplicate of that .. the OP is worried that type safety is lost when you assign an untyped empty array to the field (which is not and is type safe) – Titian Cernicova-Dragomir Sep 28 '18 at 11:06
  • 1
    @TitianCernicova-Dragomir Sorry just a wrong click – Vincent Aug 07 '19 at 16:29

6 Answers6

13

The type information is determined by the type annotation on the field (exampleArray: CustomType[]). At runtime Javascript arrays are untyped anyway. The compiler will allow an empty array ([]) to be assigned to anything as this is considered safe, since there are no objects inside, it can be an array of CustomType. The field type will then prevent you form pushing to the array objects of any other type:

class CustomType { x: number}
class Example {
    public exampleArray: CustomType[];
    public clearArray() {
        this.exampleArray = [];
        this.exampleArray.push(new CustomType())
        this.exampleArray.push({}) // error not a `CustomType`
    }
}

Note

If this had been a variable with no type annotation, the variable would have been inferred to any[], which can lead to problems (types are not checked on either when assigning an array literal or when you would push to the array):

let noAnnotationArray = [] //  any[]

In this case, adding a type annotation is still the best way to go. Type assertions (as suggested in other answers) can lead to uncaught errors if later someone adds an item to the array:

let withAnnotation:CustomType[] = [{}] //  error
let withAssertion = <CustomType[]>[{}] // no error even though we assign {}
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
8

This post seems to be out of date. There is a new way to do this.

this.array = [] as CustomType[];
Luke Becker
  • 864
  • 7
  • 14
  • Why use a type assertion ? the type of the array is already determined by the annotation on `array`. The `as` is equivalent to the `let withAssertion = [{}]` which I explain why is a bad idea – Titian Cernicova-Dragomir Aug 07 '19 at 12:53
  • Thanks @Luke Becker. What the comment above says is wrong, because the as is for the [] instance you pass as new array, so thanks!. – Guille Feb 18 '21 at 16:11
4

There can be 3 ways of emptying an array

  1. setting its length = 0

    myArr.length = 0;

  2. using splice method Array

    myArr.splice(0,myArr.length);

  3. Pop() each element of array

    while(myArr.length){
      myArr.pop();
    }    
    
Sarthak Aggarwal
  • 2,284
  • 1
  • 8
  • 12
3

You can always clear the list items by changing the length property to 0

eg: this.exampleArray.length = 0

Setting length to 0 clears all the elements in the array without altering the reference of the array.

Punith Mithra
  • 608
  • 5
  • 9
2

Clean the array even if is passed as paramener through a method::

myArray.length = 0;
Radu Linu
  • 1,143
  • 13
  • 29
1

You could try with

this.exampleArray = <CustomType[]>[];
Juan Miguel S.
  • 591
  • 2
  • 11