Suggestion: minification
TypeScript should support emitting minified JavaScript.
There are several different things we could support:
- Just remove whitespace
- Minify unobservable identifiers
- Remove provably dead code
- Whole-program minification (i.e. closure compiler)
- (Others?)
@evmar My understanding is that Closure's type system is nominal and that @implements is required, I may be wrong (or out of date), but if so that is not even a subset of TS, it is a complete divergence on both the technical and conceptual level and fundamentally incompatible.
I think we're in agreement, then! My point is that you must be fundamentally incompatible if you intend to optimize, and it's just a question of how much incompatibility you're willing to tolerate.
For example you could imagine compiling code that uses TS structural interfaces in a purely nominal way into an optimized form and fall back on code that relies on structural typing to generate unoptimized code. (Also for this particular point the Closure type system has some structural typing support, but the general idea stands: worst case you can just do what uglify does.)
On Fri, Dec 30, 2016 at 10:47 PM, Aluan Haddad [email protected] wrote:
@evmar https://github.com/evmar My understanding is that Closure's type system is nominal and that @implements is required, I may be wrong (or out of date), but if so that is not even a subset of TS, it is a complete divergence on both the technical and conceptual level and fundamentally incompatible.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Microsoft/TypeScript/issues/8#issuecomment-269852720, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAPBxnlu1brJR1vk_sOYFKP8IG1TK4mks5rNfqNgaJpZM4CNZxl .
Fair enough, but that simply is not TypeScript. It is Closure.
I am not going to start writing Closure. I know you are not asking me to do that in any way, but bear with me.
I think people should be aware of what they are getting into.
Treating TypeScript as a syntactic veneer over a language other than JavaScript sounds very dubious.
Closure is a subset of JavaScript which, from what I can tell, is based on Java. Frankly, trying to use a language that didn't even have a way to pass one method to another until 2014 as a basis for statically analyzing JavaScript seems a little off in left field.
Closure compiler is a compiler for JavaScript. You write your JavaScript with annotations in order to gain static code analysis for a dynamic language. The main difference is the modeling. Closure Compiler knows interfaces, classes, polymorphism and all OO paradigms. These concepts are expressed within the structure of your code, therefore you can (don't have to) define interface implementations. If you write your code in Java, C#, (same name for class and interface) or try to compile it in closure compiler you will get an error. In my opinion, defining the same name for a class and an interface is wrong even if the language allows it. It may create confusion and may shadow the main functionality of the class. In addition, I don't understand why not to define the property as part of the class. It is so strange that even the TypeScript compiler does not warn you in case you don't implement the contract of the interface Greeter, so why bother to write an interface?
Here is a use case
interface Product {
readonly name: string;
readonly price: number;
readonly inStock: boolean;
}
class PowerSaw implements Product { // Error
constructor(properties: Product, readonly blades: Blade[]) {
Object.freeze(
Object.assign(this, properties)
);
}
}
function addToOrder(product: Product) { ... }
addToOrder(new PowerSaw(...)); // Error
By adding
interface PowerSaw extends Product { }
both errors are fixed.
@aluanhaddad indeed, if you are piping TypeScript through Closure compiler, you are restraining yourself to write in the common language subset of TypeScript and Closure compiler, or more specifically, the language subset your TS->Closure translator enables you to use.
Whether that's practically useful depends on the size of that subset, and a slew of less fundamental and more pragmatic issues (error behaviour, debuggability, optimization quality, etc). An interesting question is also the inverse, that is whether it's pragmatic not to use Closure compiler.
I have first hand experience with several applications where using Closure Compiler as an optimization step after TypeScript is pragmatically useful, and not using would be prohibitive (due to code size). Whether the same is true for you of course depends on your use case and many other factors.
FWIW, the nominal vs structural issue is not actually a problem, Closure compiler supports structural interfaces using @record, and the types vs values namespace issue can of course be fixed by escaping interface names.
I'm partial to having a preset in typescript to emit Closure compatible JS. Basically save types as Jsdoc, check for a couple things that prevent advanced optimizations, and run the right emit settings.
IMO, a tool in the middle shouldn't be necessary. Typescript already has special support for react and different flavors of JS, adding explicit support for Closure would be a great selling point for typescript.
It really just depends on how important it is to the Typescript team. I have a feeling the need for dead code elimination is only going to increase as js gets more widely used, plain minification isn't going to be enough forever. Closure is hands down the best tool for this and the type system is largely compatible already.
@evmar I looked over the tsickle repo and the open issues to see what it currently offers as I may well have to use it at some point in the future.
It seems like there are some rough edges but the commitment that you have shown to abstracting away the differences is really impressive. It seems you have made it a fundamental design priority that the inherent mismatches between the two typesystems be handled internally within tsickle. This is not trivial. Clearly you value developer experience and, especially as some frameworks don't place enough value on it, I really appreciate it. Thank you ❤️
@MGunlogson I think long term the best place for us would be as some sort of plug-in emitter rewrite pass (using the same mechanism TS uses to downlevel async/await, or downlevel ES6->ES5). But I say plug-in because don't think the TS team wants to own it, as there's a lot of finicky details in making Closure work that unfortunately will require a lot of time. Depending on how you look at it the type systems are either "mostly compatible" or "fundamentally incompatible".
@aluanhaddad thanks! I would add that I don't think Closure minification is necessarily the best solution for TS, but rather that it's a pretty good local maximum. And we're particularly committed to it because we're making TS interop with a large body of existing Closure code.
@RyanCavanaugh
This has been sitting in "Needs Proposal" because, as I've outlined here many times, we're looking for someone to present a compelling case that TypeScript itself can do a substantially better job than off-the-shelf minifiers, in a way that is general-purpose enough to work for most people.
Proposal: Typescript has a tsconfig option "minifyPrivateMembers"
One compelling case is any front end data binding framework such as Angular, Knockout, React, etc. Through data binding we have references to javascript variables in html code. It'd be really nice to be able to mark things as "private" and have those be minified.
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. Perhaps something could generate an externs file out of everything that's not marked private and then run closure compiler, but it'd be much nicer if typescript could just minify anything marked private. Typescript wouldn't even have to minify anything else, uglify can do the rest.
I have a library using typescript with knockout, and it's 418,828 bytes with uglify and 302,158 bytes with closure compiler. That's about 28% smaller. I'd very roughly estimate real savings would only be half that, since that closure compiler result also obfuscated public things.
@mblandfo , can you submit a more formal proposal as a separate issue? I think that has a better chance of being noticed by the core team, and would provide a better place to discuss the specifics of what you're after.
I think just having that one setting would be enough - especially since we now have a plugin compatible compiler, the time has come for this to see the light of day.
I think tsc should definitely support this, since it can do related optimization better than 3rd party tools. For example, using enums result in a bunch of useless code, adding string mappings and whatnot.
However, a minifier option would imply no undeclared external references. tsc would then be able to make optimizations like replacing all enum usage with numeric literals.
@mrkite Did you know that const enums exists?
Decorators
Currently, it is impossible to use decorators with variable-renaming minification, as neither UglifyJS nor closure-compiler would rewrite property/method names in __decorate calls correctly.
This issue should be closed. It would be amazing to have a typescript-aware minifier but when an issue is 5 years old nothing is going to happen. Leaving it open just fills my heart with unnecessary hope.
Pls add this feature i cri everyday. owo
Maybe it'll be useful for someone - I've written a transformer, which (I believe it'll rename privates at all to random name by itself in the future) renames private class' members in a form of _prefix_originalName (where _prefix_ can be changed and by default is _private_; originalName - original name of the private member), so you can use terser/uglify with a mangle option to minify private properties/methods.
After smoke testing it allows me reduce application's size up to ~15% min and ~5% min.gz (from 43kB to 41kB min.gz or from 186kB to 157kB min in my case).
- https://github.com/timocov/ts-transformer-minify-privates
- https://www.npmjs.com/package/ts-transformer-minify-privates
This experimental project is using Typescript's type information for the minification https://github.com/angular/ts-minify
I would be curious to hear if anybody successfully uses https://github.com/angular/ts-minify ... Looks like the last (real) commit was 4 years ago. 🤔
I believe ts-minify was an intern project experiment that never(?) was used in production. The intern host (rkirov, some commits on that project) has since worked on tsickle, which we still support (discussed upthread on this bug).