Ignore unused methods

Topics: General
Jun 8, 2014 at 5:02 PM
Edited Jun 8, 2014 at 5:03 PM

would it be possible to have a compiler flag that would only compile the methods that are actually called starting from a given "main". I understand that solving this kind of problem in general is extremely difficult, especially in TypeScript / JavaScript. But I can imagine it would still be useful even if this feature only works for well-behaved applications.

To give you the background of my question. The main difficulty with huge mammoth libraries is that you carry around 300K of JavaScript code that aren't really used. The GUI libraries are especially prone to that. This means longer download & start-up time.

For example if you compile the following code with --entry Application::run the method A::h would not be present in the final JavaScript code.
class Application {
   public run() {
      var a = new A();

class A {
   public f() {
   public g() {
   public h() {
Thank you and best regards
Jun 9, 2014 at 8:09 PM
We've considered adding optimizations that the user could opt-in, like minification and removing unused functions/vars.

One catch here is that starting from a main and traversing from only works for a very specific to a development style. TypeScript apps are not compiled using a closed-world, perfect knowledge approach, but instead assume having only limited knowledge of the outside world. As JavaScript files are loaded on the page, they're working with an open, dynamic runtime. Just to get code to compile, users are only required to 'declare' enough of the outside world symbols that the program typechecks. There isn't a way to enforce that users do this more completely, which is unlike languages like C/C++.

We could introduce annotations to help with this, and that's a possibility.
Jun 30, 2014 at 3:11 PM
jonturner wrote:
We've considered adding optimizations that the user could opt-in, like minification and removing unused functions/vars.
Ignore unused top-level items, e.g. classes, functions, vars at least? (I'm unsure about methods.)

Simple use-case is "utils.ts" which contains a lot of functions and helpers, but simple application could be using only few util functions. In this case there is no good way to reference utils.ts cause typescript compiles all utils to the simple application even though I'm using only few of them. Only way at the moment is copy & paste only the used helpers from utils.ts to the main application. Not convenient.