Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slowly move to / migrate to TypeScript in an existing JavaScript system

Tags:

typescript

We have an already existing JavaScript system.

What we want to do: We would like to start to integrate TypeScript into the current system; We cannot just move everything to TypeScript. We want to slowly start writing some of the new modules in TypeScript.

What we tried: We use a pattern for organizing our JS code simillar to the MODULE TypeScript construct.

We tried to rewrite a simple class/object in TypeScript and were successful but we had trouble accessing JS functions defined in our code, in other files.

Problems Encountered: We had to create dummy interfaces, and dummy functions using those interfaces etc.

So the question: can anyone comment, what would be the best approach to slowly integrate TypeScript into an existing JavaScript system.

like image 587
Greg Bala Avatar asked Nov 12 '12 18:11

Greg Bala


People also ask

Is it hard to switch from JavaScript to TypeScript?

Converting a JavaScript codebase over to TypeScript is, while somewhat tedious, usually not challenging. In this tutorial, we're going to look at how you might start out. We assume you've read enough of the handbook to write new TypeScript code.

Should I move from JavaScript to TypeScript?

Compilation errors JavaScript is only capable of seeing these errors at runtime so you are highly likely to have much slower debugging because you are now doing more unnecessary checking. The better tooling available in TypeScript provides a far better experience when writing code.

Can you mix JavaScript and TypeScript?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).


2 Answers

It sounds like you have a sensible plan. Here are my observations!

It is best to make sure you TypeScript depends on your JavaScript and not the other way around where possible. This is because it is very easy to refactor TypeScript using the Visual Studio tools, but it won't refactor JavaScript that calls your TypeScript.

You will have to write definition files for the parts of your JavaScript you need to call from TypeScript. You will need to balance the cost of writing a definition with the cost of simply converting the JavaScript to TypeScript.

If you are only calling one function, just write the definition for that one function - don't write a definition until you need it. This will keep the cost of calling your old code lower.

You could also temporarily use the any type to get away with calling anything on your JavaScript code. When you convert the file to TypeScript you will get better type checking. This is an "it depends" decision point. Rather than spending ages writing a definition, you could save the time at the cost of type checking.

For example...

declare var MyExistingClass: any; 

You can now call...

var example = new MyExistingClass.Anything(); example.anythingYouLike(); 

You have to decide as a team whether this is acceptable or if you want to write definitions:

declare class MyExistingClass {     anythingYouLike(): void; } 

I hope this helps.

like image 188
Fenton Avatar answered Sep 25 '22 02:09

Fenton


This is something we have recently faced moving an HTML5 game engine of about 100,000 lines of JavaScript to TypeScript. We necessarily had to do it in stages, starting by just renaming the files from .js to .ts and gradually proceeding from there. The full description is here for anyone that is interested:

http://hardcodeded.blogspot.jp/2013/02/mostly-painlessly-migrating-3d-game.html

like image 32
duncan Avatar answered Sep 24 '22 02:09

duncan