5

I'm preparing for the js13k game dev competition and have been searching for ways to minify TypeScript code. Base on this SO question from 4 years ago, the solution seems to be compiling TypeScript code to JS and use the regular js tools but in doing so you lose all the valuable type information.

Is there any way to make use of TypeScript's types to better minify and optimize the code's output?

  • TypeScript's output **is** JavaScript. And of course the typing information is lost, because only TypeScript's compiler cares about the types. To answer the question; I'm sure there is a way, but explaining it is likely to take far more words than are allowed in an answer on Stack Overflow. – Heretic Monkey Jul 17 '17 at 16:52
  • I don't think that there's an existing tool for that. There's a suggestion to [make it part of the language](https://github.com/Microsoft/TypeScript/issues/8). I'm pretty sure that you're best (and only) option is to use js minifyers, and there are some good ones out there, for example take a look at the google closure compiler – Nitzan Tomer Jul 17 '17 at 16:58
  • I have looked into the Closure compiler before but that requires you to write [JSDoc comments](https://github.com/google/closure-compiler/wiki/Annotating-JavaScript-for-the-Closure-Compiler) containing type above everything which seems pretty inefficient and messy when TypeScript syntax has the types. –  Jul 17 '17 at 17:35
  • Well, try [TypeDoc](http://typedoc.org/) to produce the comments from the types, maybe then you'll be just able to use closure? I've never used any of those so I can't say for sure, but it might be worth looking into – Nitzan Tomer Jul 17 '17 at 19:52
  • Why don't you just compile your Typescript to Javascript and then minify it? – Kokodoko Jul 17 '17 at 22:33
  • @NitzanTomer Thanks Nitzan, this seems to have have a lot of potential since then I could use the closure compiler with all the type information –  Jul 18 '17 at 16:54
  • I heard some people compile to ES6 or Babel code and then use an optimizer for those languages, since ES6 is higher level than ES5, you theoretically could get better optimizations. – Berty Mar 22 '18 at 15:12

1 Answers1

2

I know this question is two years old, but I ran across this question looking for something similar and this is what I've found so far (note that the latter two, at time of writing, both are works in progress):

  1. typescript-plus: a TypeScript compiler that includes some additional compiler features, including some optimizations.
  2. ts-optimizer: a proof-of-concept TypeScript optimizer.
  3. Tsickle + Closure Compiler: Tsickle is a code transpiler from Google that converts TypeScript code into JavaScript code that is acceptable to Google's Closure Compiler. Closure compiler then analyzes your code and strips unused portions then optimizes and minifies the rest. The Closure Compiler is capable of leveraging type information via its own variation of JSDoc annotations.
apokaliptis
  • 424
  • 1
  • 4
  • 12