Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make constructor static in TypeScript?

Tags:

I read about a static constructor in TypeScript and tried it myself but it doesn't work. I want to initialize a static variable by that (the method shall only be called once) but I get the following compiler error:

Error: 'static' modifier cannot appear on a constructor declaration.

Code:

export class DataManagement {   private static subjects: string[];    static constructor() {     DataManagement.subjects = [];     //some more code here   } } 
like image 649
TheProgrammer Avatar asked Mar 31 '18 15:03

TheProgrammer


People also ask

Can TypeScript class have static constructor?

While other languages like C# do have static constructors, TypeScript (and JavaScript) do not have this feature.

Can I make my constructor static?

Java constructor can not be static One of the important property of java constructor is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.

Does TypeScript support static?

TypeScript doesn't allow a static property or method to be affected by an object instance. We can instantiate the object just fine, but if we try to access the property or method, TypeScript will raise an error.

Does TypeScript have static classes?

In TypeScript, we don't have a construct called the static class , and a class with only one instance is often represented as a regular object. We don't need static class syntax in TypeScript because a top-level function (or even an object) will do the job just as well.


1 Answers

While other languages like C# do have static constructors, TypeScript (and JavaScript) do not have this feature. That said, you can still define a function statically which you invoke yourself. First, let me explain in which cases you might need a static constructor before I go over the options you have.

When to use a static constructor

A static constructor is useful in cases in which you need to calculate the value of a static attribute. If you just want to set an attribute (to a known value), you don't need a static constructor. It can be done like this:

class Example {     public static info = 123;     // ... } 

In case you need to compute the value, you have three ways to "simulate" a static constructor. I go over the options below and set the static attribute "info" in each sample.

Option 1: Call an initialize function after the class is declared

In the code below, the function _initialize is statically defined in the class and invoked immediately after the class is declared. Inside the function, this refers to the class, meaning that the keyword can be used to set any static values (like info in the sample below). Note, that it is not possible to make the function private, as it is invoked from outside.

class Example {     public static info: number;      public static _initialize() {         // ...         this.info = 123;     } } Example._initialize(); 

Option 2: Directly invoke the function inside the class

The second option is to use a function, which is directly called after its creation inside the class. The function only looks like its part of the class, but has no relation to the class itself (except being defined inside of it), meaning that you cannot use this inside the function.

class Example {     static info: number;      private static _initialize = (() => {         // "this" cannot be used here         Example.info = 1234;     })(); } 

Alternative: Calculate a single attribute

Based on the same idea as option 2, you can calculate a single attribute by returning the value. This might be handy in cases where you only want to calculate one attribute of the class.

class Example {     public static info = (() => {         // ... calculate the value and return it         return 123;     })(); } 

What to use

If you only want to calculate a single static attribute, you might want to use the last (alternative) approach. In case you need to do more complex calculations or want to set more attributes, I recommend using option 1 if you don't mind the function being public. Otherwise, go with option 2.

Note, that there is an issue in the TypeScript repository, that contains some more discussions regarding option 1 and 2.

like image 186
Thomas Dondorf Avatar answered Sep 25 '22 23:09

Thomas Dondorf