Skip to main content

Converting a Large AngularJS Application to TypeScript Part 1

I work on a project that uses AngularJS heavily. Recently we wondered if using a preprocesser like CoffeeScript or TypeScript for our JavaScript would be beneficial. If our team is going to switch languages, we would need to be able to convert existing code over without much pain and we would have to find enough value in switching that it would be worth the conversion.

I had read an article that stated that because TypeScript is a SuperSet of JavaScript, you could convert a plain JavaScript file to TypeScript by changing the extension to .ts and not much else would need to change. I wanted to test out this claim, so I took a file that I was familiar with, an Angular Controller, and tried to convert it to TypeScript to see how much effort it would take and then try to figure out where we would benefit from using TypeScript.

This is what the controller JavaScript file looked like to start out with:
(function () {
    'use strict';
 
 angular
        .module('app')
        .controller('controller', controller);
 
 controller.$inject = ["$scope"];
 
 function controller($scope){
  ...
 }
})();

After changing the file's extension to .ts, in Visual Studio, I was greeted with Errors. Despite the IDEs telling me that there were Errors, the TypeScript file was still able to be compiled into a JavaScript file by just saving the .ts file, and the application continued to work without any changes to the file. While it was true for this file that it compiled without changing anything, the Errors would be a problem for building the entire solution.

The first Error I had to deal with was the fact that TypeScript did not know what angular was. It needed to be defined for the compiler to not complain about an object being used without any reference. Online resources lead me to discover that the "correct" way to solve this problem is to reference a definition file at the top of your TypeScript file. This is treated like a C# import, telling the compiler about that object. However, because I am trying to first do as little as possible to convert this file, I searched for and was able to find another approach that allowed the compiler to recognize angular without the need to reference any external files.

This is one area where the fact that TypeScript allows typing to be optional really helps out. I was able to declare the angular object as type "any". TypeScript sees the "any" type and allows for anything to be called on it, or for it to have any properties. This is basically like telling TypeScript to treat the object as a normal JavaScript object, which provides a lot of flexibility, but doesn't give you the benefits that TypeScript offers. In this case, the flexibility is worth it and we end up with:
declare var angular: any;

(function () {
    'use strict';
 
 angular
        .module('app')
        .controller('controller', controller);
 
 controller.$inject = ["$scope"];
 
 function controller($scope){
  ...
 }
})();

Next Error is that TypeScript did not like the Angular $inject on the controller. Because the controller is not defined it treats it as a function, and functions don't have a $inject property. So I needed to similarly define the controller as an "any" object so that the compiler would stop complaining. This got rid of all of the Errors and we are left with this:
declare var angular: any;

(function () {
    'use strict';
 
 angular
        .module('app')
        .controller('controller', controller);
 
 controller.$inject = ["$scope"];
 
 var controller: any = function($scope){
  ...
 }
})();

Unfortunately, after making this change, the application no longer works and I got JavaScript errors thrown by the browser. It treated controller as undefined at the point where it tried to add it in the angular .controller initialization. Surprisingly to me at least, defining the controller as a variable that is a function after trying to use it fails. The solution is to move the declaration up so that it happens before the controller is referenced anywhere. Leaving us with:
declare var angular: any;

(function () {
    'use strict';
 
 var controller: any = function($scope){
  ...
 }
 
 angular
        .module('app')
        .controller('controller', controller);
 
 controller.$inject = ["$scope"];
})();

So in the end I had to make some changes, although they were very minor, to be able to get Visual Studio to compile without build Errors. With this conversion we don't get any real benefits from switching to TypeScript. Next up is to start digging into the features of TypeScript and find out how much value we can get from utilizing those features.

Comments

Popular posts from this blog

Converting a Large AngularJS Application to TypeScript Part 2

In part 1 I was able to take an Angular controller written in JavaScript and convert it to a TypeScript file while doing very little to change the code. In this post I am going to explore transitioning that same controller to actually use the features provided in TypeScript. This is how I left off my controller:
declare var angular: any; (function () { 'use strict'; var controller: any = function($scope){ ... } angular .module('app') .controller('controller', controller); controller.$inject = ["$scope"]; })();
While performing the translation from JavaScript to TypeScript, I would make sure at every step that the functionality I expected still worked, so if anything I did broke the system I would change it back and try again with another approach. Also if something seemed like it worked too easily, I would break it on purpose to make sure I wasn't getting a false result through browser caching a previously working fil…

Gamify TDD

I like it when things that would not normally be associated with games add concepts from games as a way to incentives you to accomplish things. Why simply go for a run if you can have an app that will track you and give you a gold star if you do better than you did the last time? Why go to the coffee shop that only gives you coffee if the other one will give you points that you can redeem for free drinks eventually?

I was recently introduced to CodeSchool, an online training system similar to PluralSight, it has video courses and challenges you can take to prove that you retained what the video taught. CodeSchool also adds badges and tracks to your learning, so as you complete a video and its challenges you get a badge. Complete a collection of courses within a specific discipline and you become a master of that discipline.

Some of these incentives are not tangible and really don't mean much in the real world, but they tend to work for me. If I start working towards a large goal a…