Skip to content

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

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

classMyHappyClass{staticmyStaticProp='this is a simple static prop';staticmyStaticMethod(){return'static method'};}

Expected behavior:

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

Actual behavior:

The code above downlevels to the static prop being initialized

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

Playground link

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions