Tass Iliopoulos

TypeScript and Visual Studio

Lately I’ve made some changes in how I organise TypeScript files in my web projects.

For any code I’ve written, I don’t commit the generated .js files into source control - just the .ts files. I have a couple of tools around to finish the job:

  • When I hit ‘save’ on a ts file, the excellent Web Essentials will create a .js file. Great for on-the-fly changes.
  • When I hit ‘build’, a pre-build step runs and compiles all .ts files.
  • When I commit the .ts to source control, the build server will generate all the .js files, ready to deploy.

There are benefits to keeping the .js files out of source control

There are no js files to be left out-of-date

It’s easy to forget to commit both the ts and matching js files.

There are less conflicts to deal with

A conflict will almost always appear in both the ts and js files. Why double the amount of work resolving them?

Version mismatches

Anyone running an old or modified version of the typescript compiler won’t break things for anyone else.

There are also pitfalls

It’s not obvious if someone is running an old TS version

This could result in code working on some developers machines but not others. Will something like this be possible in the future?

#if tsversion < 0_8_0_1
 don't build!

Web Essentials for Visual Studio is overly persistent

Every resulting js file is added to your project. This is great when you’re committing your js files together with your project. When you don’t want these committed, it’s quite annoying.

Update: There is now an option in Web Essentials which controls whether the generated files are added to the project. Success!

This is the best way to work by far

The pitfalls are, overall, very minor and I’m quite happy with this project structure.