Thoughts on TypeScript

Over the past few months I’ve started working pretty extensively with TypeScript. For those of you who don’t know what TypeScript is:

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

It provides strong types to JavaScript. It allows for the creation of classes and enforces those classes in your code. If you define a Redpill.Widget class, you can then use that class in your code and the editor enforces the rules you define within that class. For example, if you define a findFoo method that accepts a string. If you attempt to pass a number to findFoo your editor will complain about it.

To me, TypeScript feels a lot like Java. There are a lot of the same paradigms and ideas around the things you can create (Enum, Interface, Map, Class, etc). The use of TypeScript doesn’t mean you’ve wasted your time learning JavaScript. On the contrary, your time learning JavaScript has been leading you to TypeScript. Everything you’ve learned about JavaScript translates to TypeScript, because at the end of the day, TypeScript IS JavaScript, just with a lot of structure around it.

One of the benefits of using TypeScript is you can now get rid of your linters. Yes linters have their place, however since we’re not more strongly typing our JavaScript and TypeScript provides more editor niceties, we really no longer need a linter. I would say the only linter you really need once you move to TypeScript is a JSCS configuration to enforce your coding styles.

With the tight editor integration you also get really smart typeahead support. This makes writing code much easier and also provides your editor a means of navigating to class, method and property definitions with a click of the mouse, even if they reside in other files. You also get your linting done as you write your code. Almost all of the current text editors have a TypeScript plugin to provide this sort of support.

My experience so far is that I like it, a lot. I can now create model classes that deal with a single thing. I can also define interfaces and have proper class extension. My editor then informs me of the properties and methods of that class or interface as I’m using it, which is a really big deal in my eyes. Yes, I could create a model class with JavaScript, but it really isn’t extendable. I can extend my model classes or create abstract classes and interfaces, which I really couldn’t do with JavaScript (at least not at my current skill level). TypeScript is highly scalable and is intended for large projects where JavaScript really was never intended for really large projects as it’s a scripting language.

If you have the time and are wanting to learn something new, take a look at TypeScript. I think you’ll find it easy to transition to in place of JavaScript (well it is JavaScript). The features you get while writing code are just excellent and quite a pleasure to work with.

Until next time…. Happy Coding

Here’s an introductory video¬†

Share This: