In this post I want to talk about how I do virtually all of my JavaScript development - spoiler alert… I don’t write JavaScript… I write TypeScript and compile it down to JavaScript.
What is TypeScript?
TypeScript is a language introduced and run by Microsoft that has a goal to make it easier to build and manage large enterprise scale JavaScript applications. It does this by adding some semantic sugar to JavaScript to make the development experience better.
Semantic Sugar? Yeah, I keep hearing that but no one seems to explain it when they say it. To me, it means they have added some stuff to JavaScript in order to help when building large applications. What sort of stuff? Some things are included in the next version of JavaScript (EcmaScript 5 6) like classes & modules. Some things are proposed for future versions of JavaScript like interfaces & annotations while even other things have been flagged for even further out things.
TypeScript compiles down to JavaScript so your runtimes & clients don’t have to install anything special… they already have JavaScript runtimes so there’s nothing for them to do. It’s like writing C#… you don’t deploy the C#… you deploy the compiled output.
Why TypeScript?
But what does this stuff do? Without strongly typed variables & objects, you frequently don’t find out if there are errors until you run your tests. Many of these same issues could have been caught by a compiler, editor or IDE.
But catching your possible coding errors isn’t the only reason why it’s helpful or why I like it. When you are developing the IDE / editor you’re using can use autocomplete / IntelliSense just like you get in strongly typed languages. It can also give you hints for the return type of methods and functions as well as tell you what parameters the function takes as well and their types.
One use cases I like to share with people who are coming from a strongly-typed language background. So you need / want to write JavaScript for your web app or you want to kick the tires of Node.js… TypeScript is a great way to move into that world. Some of the more challenging aspects to JavaScript development for those new to the language (see strong typing, classes, IntelliSense / autocomplete above).
When / Where do I use TypeScript?
This past weekend when I was presenting a session at the Atlanta SharePoint Saturday event on TypeScript I got this question… when & where do I use TypeScript. Honestly? Everywhere I write JavaScript. I use it when I do client-side development, server-side development using Node.js and even when I write my Gulp tasks.
The latter case of writing Gulp tasks using TypeScript is a bit interesting because I usually use Gulp to compile my entire project from TypeScript to JavaScript. In that case I have a bit of a chicken & egg issue: The Gulp file that has my tasks must be in JavaScript in order to run. So I usually write it in TypeScript but either manually compile it using the tsc.exe
command line compiler or I let my editor compile it for me.
Parting Thoughts
How easy is it to get into TypeScript? In my experience it’s easy to listen to a presentation or read an article, but once you go try it within a project, you start to bump your head on some low ceilings. My recommendation: don’t try it out for just a few hours… you need to make an investment and say you’ll stick with it for a solid week before you bail on it. Make sure you try to do everything the “TypeScript-way” to really get a good feel for it. Once I did that, I had a much better grasp of the language.
Another thing that I wanted to comment on is the adoption / interest in TypeScript. When it was first announced it was seen as just a Microsoft thing. Therefore most of the JavaScript community blew it off or ignored it. However, since March 2014 when Google & Microsoft announced they were collaborating on it (in fact, Google is writing Angular 2.0 in TypeScript), it’s gotten a whole new breath of fresh air into it. Now, in the last few months, the greater JavaScript community is interested! I’m curious to see where this goes now!