Sunday, April 7, 2013

Typescript static constructors for classes

Typescript does not currently provide a syntax for C# style static constructors. However you can get the same effect with the pair:
  • A non void static function
  • A static variable assignment used to call that function:




Foot note:  There is a request for a dedicated syntax to be added to typescript here : https://typescript.codeplex.com/workitem/862

e.g. (Non working / Proposed syntax) :



1 comment:

  1. You can shorten that code by using an immediately executed function literal instead:

    static Ctor = (() => do stuff)();

    ReplyDelete