Many people are starting to get interested in TypeScript and are asking (with some fear) questions, and how can they benefit from moving to TypeScript. So I've decided to blog about it, so everyone can gain.
What’s going on todayAccording to Wikipedia, EcmaScript 6 specifications are planned for Dec. 2014. FireFox has already implemented many of the features, Chrome are pushing hard too, we have Node Harmony, and even IE11 already implemented the Let, Set, Map and much more.
So things are looking good, and will be much better in the near future. But till then we have TypeScript (Version 1.0 was released not long ago)
Why would you want Types?If you write your code with tests (and you should), you probably know the feeling when a test fails. “Yeah, I didn't do that right; I should have sent this object and not that one.” It’s a very human thing to make mistakes, and the tests help you find them before you even run your code.
The same goes for Type Safe coding. It is actually a test that needs to pass before you run your code. If you told the test that a function must receive 2 string parameters, any call to this function should fail if it didn’t do that. That’s what happens in the compilation of TypeScript code.
Besides testing your code types, TypeScript provides many more features like classes, interfaces, generics, arrow functions and much more. All these features are planned in ES6 and are implemented in TypeScript, so once the world is ES6, you are too.
"Classes and Interfaces, Ugh!"You may argue that Classes and Interfaces and bad things, and you are very comfortable with the module pattern. This is great and you can keep doing that, TypeScript does NOT force you to do Classes and Interfaces!
In my mind, there are some situations that classes and interfaces are very welcome, and should be used, and there are places that functional programming is the right thing to do. choose your style.
"I hate generated code"Me too! most of the debugging that I do, I do it on the browser.
The TypeScript generated code is very similar to the code that you write, with some additions.
The types are omitted, the names of the variables, functions, and modules remain the same, your comments are still there, so you will have no problem debugging TypeScript generated code. Belive me, I've been doing for this for the past year with no problems at all.
3 Baby steps
But, if you find a place that you want to be more strict – for example, specify a function parameter type as string – TypeScript will help you enforce that, just like a test. And after the compilation, all the type definitions are removed and your code will be as it would have without TypeScript. You just added a test.
TypeScript implemented the module pattern and it’s very easy to use. It’s a much more organized code, and it’s loadable using commonjs or AMD .
Those days are over. From now on you should use the arrow function; it will generate the “_this” behind the scenes for you (you can see that on the generated js files).
For starters, this is more than enough to get you started and get your feet wet. So I’m not going to talk about classes, interfaces, generics, and all the other great stuff… :-)
Before you start
- Use Grunt to compile your code, I would suggest using grunt-ts as it has more features over the grunt-typescript task.
- DefinitelyTyped is a great repository for type definitions for existing libraries like AngularJS, JQuery, or anything you can think of.
- WebStorm and VisualStudio are the best IDEs for TypeScript, but if you don't like it, be sure that your current IDE supports TypeScript.
- You can play with TypeScript here
- ES6 is a great language for small, medium, and mostly large apps. Read about it; after all, it’s coming soon, and you don't want to be left behind.
The only downside that I see is the generated code, it looks very complicated and I guess that with Traceur you must use source maps to debug your code.