question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Private Member Minification Design Proposal

See original GitHub issue

Problem: For front end data binding frameworks such as Angular, Knockout, React, etc, making the choice to use typescript results in larger minified output files.

Proposal: Add a tsconfig option “minifyPrivateMembers” (default false)

Details:

Since front end binding frameworks can have references to members in html, we cannot easily minify these members using existing tooling. For example, using google closure compiler would require using an externs file for all the public properties I want to keep. That would be awful to hand maintain. It’d be really nice if typescript could just minify anything marked private. Typescript wouldn’t even have to minify anything else, uglify can do the rest.

Edit: removed incorrect example.

Minification of private members in the greeter example is 18% better than minification in javascript only. I also expect many classes would have many more private members than public members, so savings could be much greater than 18%. Actually with many custom knockout bindingHandlers almost every single member can be made private!

this["greeting"]
var x = "gr" + "eeting";
var y = this[x];

If typescript notices that a class is using string access with variable strings to its own members it could choose to not minify private members for that class. Alternatively if this feature just breaks code like that then I think that’s okay as long as it’s documented. It will already break external use of private members, and that’s why the option defaults to false.

Created from here: https://github.com/Microsoft/TypeScript/issues/8#issuecomment-303423733

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:47
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
thinwirecommented, Jan 24, 2019

This is absolutely a problem that the TypeScript team needs to address and not try to shift responsibility entirely to Javascript post-processors. The transformation is best done during compilation, when you have information about the source available, instead of having to guess at it from emitted magic comments or name formatting. Bottom line is, we do not want the private members of a base class to stop the declaration of a field with the same name in a derived class. After all, private members should only be accessed from inside the class itself. One easy way to generate these private names would be to find the depth of the class hierarchy up to that point and prefix the emitted private names with a number of prefix characters equal to depth, e.g.

class A {
    private a: number = 0;
}

class B extends A {
    private a: number = 1;
}

class C extends B {
    private a: number = 2;
}

would result in a generated class C like

function C() {
    this._$a = 0;
    this.__$a = 1;
    this.___$a = 2;
}

This would still leave the private fields human-readable (if only prefixed), non-clashing, source-mappable and targetable from an external tool via regex (matching _+\$ in this case).

Edit: an alternative that is smaller: <prefix><depth><suffix><varname>:

function C() {
    this._1$a = 0;
    this._2$a = 1;
    this._3$a = 2;
}

targetable with _\d+\$

6reactions
NoelAbrahamscommented, Dec 14, 2017

@kitsonk,

For example, --mangle-props regex=/^_/ will only mangle property names that start with an underscore.

Isn’t it better to use your mangler/minifier than to impose something upstream?

Why would you want to impose the underscore convention on anyone? It’s outdated and ugly. That’s the reason we have a private keyword.

Read more comments on GitHub >

github_iconTop Results From Across the Web

An Information Hiding Proposal for ECMAScript
It means that a private member is only accessible to other members ... A complete strawman proposal for this style of information hiding...
Read more >
@babel/plugin-proposal-private-methods - Package Manager
Fast, reliable, and secure dependency management.
Read more >
Proposal - RGPacific - Proposal Website Support Services - CA.gov
The RGP team redesigned the website with various metrics and KPI's in mind – customer profile, core audience, calls to action, and user...
Read more >
A Proposal For Type Syntax in JavaScript - TypeScript
for parameters and class members, type declarations ( interface s and type ... Things like visibility modifiers (e.g. public , private , and...
Read more >
29 Sample Proposal Templates and Design Tips - Visme
This way, you can easily get started creating your own stunning proposal design in minutes. Let's dive in! Create reports, proposals, ebooks and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found