TypeScript static classes

后端 未结 12 709
太阳男子
太阳男子 2020-12-04 13:34

I wanted to move to TypeScript from traditional JS because I like the C#-like syntax. My problem is that I can\'t find out how to declare static classes in TypeScript.

12条回答
  •  没有蜡笔的小新
    2020-12-04 14:30

    Abstract classes have been a first-class citizen of TypeScript since TypeScript 1.6. You cannot instantiate an abstract class.

    Here is an example:

    export abstract class MyClass {         
        public static myProp = "Hello";
    
        public static doSomething(): string {
          return "World";
        }
    }
    
    const okay = MyClass.doSomething();
    
    //const errors = new MyClass(); // Error
    

提交回复
热议问题