How to compile a Typescript file ?
Last Updated :
18 Jul, 2024
TypeScript is a robust, open-source programming language developed and maintained by Microsoft. As a syntactic superset of JavaScript, TypeScript enhances its foundational language by introducing additional features, including strong typing and object-oriented programming capabilities.
Unlike JavaScript, TypeScript cannot be executed directly in browsers. Instead, TypeScript files must first be compiled into JavaScript to function properly.
To compile and run TypeScript, you need to install Node.js and then use it to install TypeScript globally on your system. This guide will walk you through the process of compiling a TypeScript file.
Setup for the Typescript:
We need to download & install the node.js & node package manager (npm) to set up the environment for the typescript to work. In order to check the version installed for node & npm, type the below command in cmd:
node -v
npm -v
Note: We need not download the npm explicitly, it will automatically download & install along with the node.js.
After successful installation of node.js & npm, we need to install the Typescript using the below command:
npm install -g typescript
At this stage, we have successfully completed the environment setup for the Typescript to start work on it. Please note that we have to create & keep the typescript file in the same directory where we have installed the typescript. Save the typescript file with the extension of .ts & add the below code in your ts file.
Typescript:
JavaScript
var greet : string = "Welcome to";
var orgName : string = "GeeksforGeeks!";
console.log(greet+ " "+orgName);
Now, run the following command in the command prompt to compile the typescript file. This will create a javascript file from typescript automatically with the same name.
Compiling typescript file:
tsc fileName.ts
Run the javascript file:
node fileName.js
After successful execution of the above command, a javascript file will be automatically generated in the current working directory and it will contain the compiled code of the typescript file. The generated javascript code will look like the following.
Example:
JavaScript
var greet = "Welcome to";
var orgName = "GeeksforGeeks!";
console.log(greet + " " + orgName);
Output:Â
"Welcome to GeeksforGeeks!"
Example: In this example, you will see the complete steps for the installation, configuring the directory, compiling & executing the ts code, that we have followed.
- Create a folder and name it Typescript.
- Open the command prompt and go to the Typescript directory and run the following command.
npm install -g typescript

Create a file index.ts and add the following code. We can follow either of the syntaxes to write the code in Typescript.
Typescript:
JavaScript
const character = "GeeksForGeeks";
console.log(character);

Now we have to compile our typescript file to javascript, therefore run the following command.
tsc index.ts
Output: Now, the typescript file is compiled and we can have the index.js file in our directory, so we will check out the output by typing the below command.

node index.js
Output: From the above image, we can see that the javascript file is successfully running and show the respective output.
Similar Reads
How to Import another TypeScript Files ?
To use code from one TypeScript file in another, we use the module system introduced in ECMAScript 2015. This allows us to export functions, classes, or variables from one file and import them into another. By doing this, we can reuse code from previous projects without having to rewrite it. This ma
4 min read
How to compile multiple Typescript files into a single file ?
In this article, we will learn how to compile multiple Typescript files into a single file. There are two approaches that can be followed here: Approach 1: Compiling multiple Typescript files into a single JavaScript file. We simply use the following syntax: Syntax: tsc --out outputFile.js typeScrip
3 min read
How TypeScript Compilation Works ?
TypeScript is a superset of JavaScript that adds type safety to your code. It compiles into plain JavaScript, allowing it to run in any JavaScript environment. The TypeScript compiler (tsc) checks the code for errors and then converts it into JavaScript. During this process, all TypeScript-specific
4 min read
How to import a module in Typescript ?
Before starting with importing modules, first of all, we need to know the basics of modules in TypeScript. We know that JavaScript has come with the concept of modules from the ES6 version in 2015 and by 2020 had broad support in most web browsers and JavaScript runtimes. TypeScript also shares the
5 min read
How to execute TypeScript file using command line?
TypeScript is a statically-typed superset of JavaScript that adds optional type annotations and compiles to plain JavaScript. It helps catch errors during development. To execute a TypeScript file from the command line, compile it using tsc filename.ts, then run the output JavaScript file with node.
2 min read
How to Declare an Empty Array in TypeScript?
TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec
2 min read
How to use TypeScript on backend ?
TypeScript, developed by Microsoft, enhances JavaScript by adding type safety, which significantly simplifies debugging and development. This guide will walk you through setting up a TypeScript backend with Node.js and Express, showing you how to leverage TypeScriptâs robust features to prevent comm
2 min read
TypeScript Control Flow Analysis
In this article, we are going to learn about Control flow analysis in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. Control flow analysis in TypeScript refers to the process by which the TypeScript compiler analyzes the flow of your code
3 min read
How to Setup a TypeScript Project?
In the world of modern web development, TypeScript has emerged as a powerful superset of JavaScript, offering static typing and improved tooling. Its strong typing system helps developers catch errors early during development, leading to more maintainable and scalable code. Whether you're starting a
2 min read
How To Get Types From Arrays in TypeScript?
In TypeScript, arrays are a common data structure, but sometimes it's necessary to extract the types of elements stored in an array for type-checking or validation purposes. TypeScript provides several ways to extract types from arrays, enabling more type-safe operations. We will explore different m
4 min read