How to Transform TypeScript Code Into JavaScript Code
- Transform TypeScript Into JavaScript Code Using Online Editor
- Transform TypeScript Into JavaScript Code Using a Code Editor
- Transform TypeScript Into JavaScript Code by Installing TypeScript Globally
Several ways can convert a TypeScript file to a JavaScript file to be effectively used anywhere required. The web browser would not allow running .ts
or TypeScript files; therefore, it is essential to convert the TypeScript file to a regular JavaScript file to run it in the web browser.
Though TypeScript is needed to be converted before being used on the web browser, even frameworks and libraries such as React.js and Angular encourage programmers to use TypeScript rather than using plain JavaScript when developing the code.
The main reason why TypeScript is much more encouraged when writing code rather than using plain JavaScript is that TypeScript code will add static type definitions for JavaScript. TypeScript also validates that the code will work correctly and be compatible with all web browsers and platforms and ensures that it is maintainable across multiple themes.
When using drag.js and Angular, it is unnecessary to convert TypeScript to JavaScript, as it is done in the background using the Webpack or another bundle. But, when only using standalone JavaScript, it is necessary to convert TypeScript to JavaScript, and it can be done using several methods.
Here are the three main methods.
Transform TypeScript Into JavaScript Code Using Online Editor
The first and easiest method is to convert the TypeScript to JavaScript code online. We can do this using the TypeScript website or any other online editor or an online tool.
When using the TypeScript website, use the Playground Tool in tools, or directly go to the TypeScript Playground. Here the only step is to copy and paste the code into the given editor, and the code will be converted directly to JavaScript.
Furthermore, it is possible to run the code and see whether it does as it is supposed to.
Transform TypeScript Into JavaScript Code Using a Code Editor
Another method that can be used is to use a good code editor like the Microsoft Visual Studio Code and then install extensions that can be used to convert TypeScript to JavaScript. For example, the Compile Hero
extension can be used to achieve the above function.
Transform TypeScript Into JavaScript Code by Installing TypeScript Globally
The third way is to install TypeScript globally on the computer and NodeJS. Similar to npm
or yarn
, it will get installed for the package manager.
After that, the TypeScript utility can be accessed using the terminal, and commands can be used to convert the TypeScript file to JavaScript. Furthermore, it is possible to watch the TypeScript file for the changes by using this method.
Additionally, the utility will give a command to create a TypeScript configuration file.
To install TypeScript using yarn
, first, open the command line on the desired folder and type yarn global add typescript
and Enter, or install the npm
package type npm install -g typescript
in the command line and press enter. After that, to create a TypeScript configuration file, use tsc --init
on the command line and press Enter, and then a tsconfig.json
would be created.
When a configuration file is created, it will be present at the root level. Therefore, the settings in the configuration file can be altered to suit the likes of the user, and the TypeScript to JavaScript conversion will be based on the configuration file.
To convert TypeScript to JavaScript, open a terminal on the folder with the TypeScript file and type tsc FileName.ts
. Here, the FileName
mentioned in the above code should be the file name of the TypeScript file that is converted to JavaScript.
When the above code is executed, a new JavaScript file will be created in the same folder, which is the conversion of the TypeScript file.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.