Friday, January 31, 2014

Angular vs. TypeScript

I have been doing a lot of interesting work recently, so I haven't had a whole lot of time and energy to do any technical blog posts, especially since my only readers are search engines. But since I am generating so many questions I will try and put down some of the issues and decisions.

So my project is using Angular JS, a JavaScript framework from Google, and TypeScript, a JavaScript language extension from Microsoft. And of course, combining Google and Microsoft causes my system to give off sparks... I will try to talk about some of those sparks.

Angular and TypeScript are trying to do very different things, but in some ways they are both trying to answer the question, "How do I structure a large JavaScript application?" The answer that they come up with is based in a different set of ideas.

Angular comes from web developers who love testing. Their answer is MVC and dependency injection. They divide the model and view brilliantly and have an awesome controller mechanism. They also have a dependency injection system and envision systems as a collection of services with each service relying on other services that are injected. It is a nice vision, but their seem to be several issues with it that I will mention later.

TypeScript comes from Anders Hejlsberg, the development lead for C#. His approach is based in how you structure programs in C#, with namespaces and object types. His approach also is in a way to force class structure on to a classless language which is great if you like classes, but as you delve deeper into JavaScript you will probably start turning against classes because they reduce flexibility and require lots of extra definition.

So combining these two approaches yields something that can be a little messy. Whose approach do I use? Do I dependency inject everything? Does dependency injection really make that much sense in such a free form language as JavaScript? Do I go with TypeScript and make everything a "class"? Well, Angular is based around singleton services, but TypeScript guides me to making every class have a special constructor. Why do I need a specialized constructor for a single instance? In the JavaScript world unless you need multiple instances of a class you don't really need a constructor function so why make one?

Anyway, I see a lot of power and value in both TypeScript and Angular, but since they are both very new and not at all built to work well together I run into a lot of issues. So I will start trying to blog more consistently about the issues I run into and the decisions and solutions I come up with.

No comments:

Post a Comment