Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static Array in typescript

Hi I am new to Typescript and I need to use static array in my code. But when I declared array as follows

private static arrayname:String[];

When I typed this. it didn't show me the array name so I can't able to push any values into this array. Can any one help me in this?

like image 441
wazza Avatar asked Sep 19 '26 08:09

wazza


1 Answers

You should use class name instead of "this":

class A {
    private static arrayname: string[] = ["a", "b", "c"];

    someFunc(): number {
        return A.arrayname.length; // Here I'm calling private statis property
    }
}


var instanceA = new A();
alert(instanceA.someFunc());

This code works for me in the Typescript playground.

Note

You can use private member inside class functions only. It isn't accessible and visible outside the class declared in.

like image 104
TSV Avatar answered Sep 21 '26 21:09

TSV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!