So I started a new project and I wanted to use Meteor as the framework and Typescript as the language in place of Javascript.

This is how I currently use them together:

  1. Open three terminals
  2. Run tsc --out client/main.js client/main.ts --watch in the first.
  3. Run tsc --out server/main.js server/main.ts --watch in the second.
  4. Run meteor run in the third.

To start working on your own project, fork meteor-typescript-seed and follow the instructions.

Why this?

My first attempt was to use the Meteor package meteor-typescript-compiler. However, it turns out that it still uses Typescript 1.0 and is slow in compilation.

It depends on the npm package ts-compiler which depends on typescript-api, which fetches and mangles the tsc.js file to export the Typescript package. The mangling does not work for version 1.3.

I tried to fix that, taking inspiration from the grunt-ts’s monkey patching.

Also, I forked meteor-typescript-compiler so that I could cut out the intermediate packages and do the Typescript mangling in the meteor package itself. Ironically, I struggled with some issues with Meteor’s erroneous error reporting.

It turns out that the vm.runInThisContext trick to eval tsc.js, which grunt-ts uses, does not work while working from inside Meteor because require is not defined in Meteor’s context. Hence, the magic/mangling needs to happen in a separate npm package.

Curioser and curioser.

With that hope, I upgraded a differnt typescript compiler: typescript-compiler to Typescript 1.3, hoping to use this as the npm package to use in meteor-typescript-compiler instead of ts-compiler.

However, it turned out that Meteor’s build system would make it very difficult for types in one file to depend on types defined in another file. If I changed a file on which several others files depended for type-checking, the decendants will not be recompiled, thanks to caching employed by meteor-typescript-compiler. The alternate was to compile each file separately each time anything changed. So, that was a no-go, in the end.

Also, I discovered, thanks to Tomas that Typescript will gladly do the task of watching dependent files which were referenced using <reference path="./file/path.ts" /> syntax. That was the time I decided to take this path.

I will look into integrating Typescript in the build system again after the Typescript team releases a stable API for their compiler.