|

What does static mean in TypeScript?

In TypeScript, the term “static” is used to describe members of a class that are shared across all instances of the class, rather than being specific to a single instance.

Static members are declared using the “static” keyword before the member declaration. These members can be accessed using the class name, without creating an instance of the class. For example:

class MyClass {
    static staticProperty = 42;

    static staticMethod() {
        return MyClass.staticProperty;
    }
}

console.log(MyClass.staticProperty); // 42
console.log(MyClass.staticMethod()); // 42

Static members are often used to implement utility functions or to store values that are common to all instances of the class.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *