TypeScript static classes

后端 未结 12 701
太阳男子
太阳男子 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:22

    See http://www.basarat.com/2013/04/typescript-static-constructors-for.html

    This is a way to 'fake' a static constructor. It's not without its dangers - see the referenced codeplex item.

    class Test {
        static foo = "orig";
    
        // Non void static function
        static stat() {
            console.log("Do any static construction here");
            foo = "static initialized";
            // Required to make function non void
            return null;
        }
        // Static variable assignment
        static statrun = Test.stat();
    }
    
    // Static construction will have been done:
    console.log(Test.foo);
    

提交回复
热议问题