Typescript allows use of static variables and methods just like modern Javascript.
Static variables and methods are declared with a keyword static
.
Static variables
Static variables exist within the class context, and are not carried forward to the object of the class.
Define a static variable like so -
|
|
The variable value is accessible using the class.
|
|
.. but not against the object.
|
|
If you need a value in the object no matter the costs, you can always have the two variables with same names.
|
|
Static methods / members
Static methods behave similar to static variables.
|
|
Note that static variables cannot be called by instance members and vice-versa.
Static variables / members are distinct from their instance counterparts at all times.