Converting existing Javascript app into Typescript

We can easily convert any javascript app into typescript and also can work side by side that is some files are in javascript and some files are in typescript. I am sharing my experience on how we do that in GhangorCloud Project. So below are the approaches that one can take to convert JS app into Typescript:

Porting whole application at once

Ideally since Typescript is the superset of javascript so if we rename our projects all .js files with .ts, that should convert our whole project into typescript but that is not 100% true. We have to face challenges here lets discuss them below:

  • The following code is correct in JavaScript, but creates an error in TypeScript:
var data={x:5, y:6}; data.z=5;

So after minor change the below code would work:

var data:any={x:5, y:6}; data.z=5;
  • The second challenge is there are many third party JS libraries that we are using in our projects. The approach deal with them is to have declaration file which will list all the library methods and variables. The file has extension as d.ts. Fortunately for most of the frameworks these declaration files are made and available at https://github.com/DefinitelyTyped/DefinitelyTyped You can search for the framework of JS that you are using .

Porting Application gradually

In this approach we would just replace JS code with Typescript code gradually. Module by module or file by file etc. In this approach,we have to write definitelyTyped declaration of our code that we are using in typescript so that typescript can access our JS files variables and data. This was the approach that we took in our GhangorCloud project and it definitely helped us and gradually replaced all the JS code with Typescript one without creating burden on our developer team

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s