Skip to content

Downlevel static properties initializer to be contained within the class IIFE to aid Uglify in dead code removal #15857

Closed
@IgorMinar

Description

@IgorMinar

TypeScript currently down-levels classes with static property initializers being outside of the class IIFE when targeting ES5. This prevents Uglify 2.8+ from removing the class from the final application bundle (after the class IIFEs have been independently annotated with the /*#__PURE__*/ annotation (see #13721) and there are no other references to the class).

Changing the down-leveling to keep the assignment within the IIFE just as static methods are down-leveled would resolve this problem and make lots of code eligible for dead code elimination by uglify (after the IIFEs have been marked with the pure annotation).

The change should be backwards compatible unless the current down-leveling approach was intentional to deal with some unusual corner-case that I'm not aware of.

TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

class MyHappyClass {
  static myStaticProp = 'this is a simple static prop';
  static myStaticMethod() { return 'static method' };
}

Expected behavior:

var MyHappyClass = (function () {
    function MyHappyClass() {
    }
    MyHappyClass.myStaticMethod = function () { return 'static method'; };
    MyHappyClass.myStaticProp = 'this is a simple static prop';
    return MyHappyClass;
}());

Actual behavior:

The code above downlevels to the static prop being initialized

var MyHappyClass = (function () {
    function MyHappyClass() {
    }
    MyHappyClass.myStaticMethod = function () { return 'static method'; };
    ;
    return MyHappyClass;
}());
MyHappyClass.myStaticProp = 'this is a simple static prop';

Playground link

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions