Javatpoint Logo
Javatpoint Logo

Migrating from JavaScript to TypeScript

To migrate from JavaScript to TypeScript, we assume that-

  • You know JavaScript.
  • You know patterns and build tools used in the project.

Let us assume we have JavaScript files that need to be converted into TypeScript. We know that, when a TypeScript file is compiled, it produces corresponding JavaScript files with the same name. Here, we need to ensure that our original JavaScript files that act as input cannot have in the same directory so that TypeScript does not override them.

From the above point, we are going to assume that our directory is set up like the below structure. Here, we will keep all the output files in an output directory called built.

Migrating from JavaScript to TypeScript

The process to migrate from JavaScript to TypeScript is given below:

  1. Add a tsconfig.json file to project.
  2. Integrate with a build tool.
  3. Moving all .js files to .ts files.
  4. Check for errors.
  5. sing third-party JavaScript libraries.

1. Add tsconfig.json file to project

First, we need to add a tsconfig.json file in our project. TypeScript uses a tsconfig.json file for managing our project's compilation options, such as which files we want to include and exclude.

In the above file, we are specifying a few things to TypeScript:

  • The include option reads all files in the src directory.
  • The allowJs option accept all JavaScript files as inputs.
  • The outDir specifies that all of the output files should be redirected in the built folder.
  • The target option specifies that all JavaScript constructs should be translated into an older version like ECMAScript 5.

2. Integrate with a build tool

We know, most JavaScript projects have an integrated build tool like gulp or webpack.

Note: Each build tool is different.

We can integrate projects with webpack in the following ways:

a) Run the following command on terminal:

In webpack integration, we use awesome-typescript-loader (a TypeScript loader) combined with source-map-loader for easier debugging of source code.

b) Merge the module config property in our webpack.config.js file to include the following loaders:

3. Moving all .js files to .ts files

In this section, we have to rename our .js file to .ts file. Similarly, if our file uses JSX, we will need to rename it to .tsx. Now, if we open that file in an editor which support TypeScript, some of our code might start giving compilation errors. So, converting files one by one will allow handling compilation errors more easily. If TypeScript finds any compilation errors during conversion, it still able to translate the code just like Word will print our documents.

4. Check for errors

After moving js file to ts file, immediately, TypeScript will start TypeChecking of our code. So, we get diagnostic errors in our JavaScript code. Some of the errors we may encounter are given below:

a) We can suppress errors with using any, e.g.:

In the below code, we can delete the error by using the type assertion.

b) Function with less or more arguments:

In the above code, the function named display() takes three arguments: name, age, and height. We call this function with two values: "Rohit," and 23. This is perfectly valid with JavaScript because in JavaScript if an expected argument to a function is missing, it assigns the value undefined to the argument.

But, the same code in TypeScript will give the compilation error: Expected three arguments but got two. To remove this error, we can add an optional parameter sign to the argument height and annotate our code as below:

c) Sequentially Added Properties

The following code is very common in JavaScript.

In TypeScript, the type of options as {} is an empty object. So, color and volume doesn't exist and are not assignable. If we instead moved the declarations into the object literal themselves, we would get no errors:

We could also define the type of options and add a type assertion on the object literal.

5. Using third party JavaScript Libraries

JavaScript projects use third-party libraries like jQuery or Lodash. In order to compile files, TypeScript needs to know the types of all objects in these libraries. We know, TypeScript Type definition files for JavaScript libraries are already available at DefinitelyTyped. So, we don't need to install this type externally. We need to install only those types which are used in our project.

For example:

For Jquery, install the definition:

For lodash, install the definition:

Once, we made the changes to our JavaScript project, run the build tool. Now, we should have our TypeScript project compiled into plain JavaScript that we can be run in the browser.


Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA