daviesgeek.com

Reservations About TypeScript

- ★

TypeScript has become a huge part of the JavaScript community in the last year or so, especially with the advent of Angular 2. Just over a year and a half ago, we were looking to make some architectural changes to our Angular application at work and we had considered TypeScript as an option. At the time, I (and our lead dev) had dismissed it, but more recently, I have begun to re-evaluate it again, particularly since Angular 2 is written in TypeScript.

The Language

I think everyone can agree TypeScript is a great language. It patches some of the shortcomings that JavaScript has suffered from for years. Features such as type checking, interfaces, and classes are great for documentation, organizational, and autocomplete purposes. All that said, I still have some reservations about the language. First, because TypeScript compiles down to JavaScript, which doesn’t have types or type checking, it’s impossible to have actual type checking. The TypeScript compiler is great at checking at compile time, but at the end of the day, it’s impossible to actually check the types.

When evaluating TypeScript, my boss and I were able to break the type checking in the TypeScript playground. Unfortunately we didn’t save it (I wish we had!) and I can’t remember exactly how we broke it. It had something to do with the setting of properties and we were able to trick the compiler into allowing a type where it wasn’t allowed. It wasn’t a terribly complex piece of code; it was relatively simple and the mistake could’ve been made unintentionally with massive side effects. I don’t know if this has been fixed in later version of TypeScript, as I can’t recall the snippet of code we used. TypeScript seems to give you the false sense of security in type checking, whilst not actually being able to enforce it. To an extent, the same still holds true today, as TypeScript still compiles down to JavaScript. It has no way of actually checking types. Obviously it’s come a long way since then, so I’m willing to give it the benefit of the doubt, but it underscores the reality of a superset language that fundamentally changes the way the base language (JavaScript) works. That brings me to my second point…

TypeScript comes with a good type system, but they're not enforceable at the JavaScript level
TypeScript comes with a good type system, but they're not enforceable at the JavaScript level

TypeScript fundamentally changes the way JavaScript works. JavaScript does not have classes. Even ES6 classes are just syntactic sugar on top of the ES5 prototypical inheritance. I’ll repeat it again. JavaScript does not have classes. The class keyword may exist in ES6+, but under the hood, it’s still just prototypical inheritance. As long as this is understood, it’s perfectly fine, but the problem comes when ES6+ or TypeScript is marketed as having “classes” in the traditional OOP sense of the word. The prototype chain has some weird stuff, such as shadowing, as well as the fact that the methods and properties on a “class” or object are actually just pointers to the originals. I’ve seen too many people really excited about classes in ES6, while not realizing that it’s not actually changing the way JavaScript works. This particular issue holds true for both TypeScript and ES6+ features. As long as the inner workings of class and prototypical inheritance are understood, it shouldn’t be a big problem, but I think it needs to be pointed out, so as to not cause confusion (and weird bugs!) down the road.

My Conclusion

Autocomplete in Visual Studio Code is a breeze
Autocomplete in Visual Studio Code is a breeze

TypeScript brings some great features, but, in my opinion, it comes with some significant pitfalls that need to be addressed and realized before jumping in with both feet.

I’m definitely more open to using it than I was a year ago. In part, this is because I’ve used Visual Studio Code and immediately fell in love with the autocomplete for TypeScript properties/method signatures. It’s such a nice change from having no autocomplete and I can totally see myself becoming spoiled using Visual Studio Code. However, I think the same reservations I had a year ago still ring true. Maybe after using it more in depth over the next year, I’ll have changed my perspective, but for now, I still have some concerns about it. Maybe this is me just worrying about something that’s not actually as big of an issue as I perceive it to be. Maybe I’ll be writing another post a year from now praising TypeScript. I’m not sure, but for now I still hold to (mostly) the same opinion I did a year ago.

Next: »