Open In App

How TypeScript Compilation Works ?

In this article, we will try to understand how TypeScript compilation works (like how a TypeScript code or a program compiles and produces an output in the browser’s console). 

First of all, as we know that TypeScript is the superset language of JavaScript having some additional features including Types additions. We may also add data types along with the variable declarations, and return types along with the function declarations (or method declarations). TypeScript is also known as Optionally Statically Typed Language since it gives an option to the compiler to whether or not to include data types associated with the variables or it basically allows us to make the compiler unaware of the type of data or variable etc. TypeScript makes JavaScript type system easier to work with and also it reduces all the problems which we as users or developers face while working with JavaScript.



Let’s understand how TypeScript compilation works?

 

Now that we have understood in detail the compilation of TypeScript let us understand all the facts using a coding example which is listed below:



Example: In this example, we will first create a TypeScript file (with suffix being kept as “.ts”) containing some logic or results which we need to output in the browser’s console.




let displayData = (
    id: number, 
    name: string, 
    field: string) : string => {
          return (id + " - " + name + " - " + field);
}
  
console.log(displayData(1 , "Aman", "CSE"));

The above code snippet when the compiler receives makes that TypeScript code converted into the following JavaScript code (file name is of “.js” itself):-




var displayData = function (id, name, field) {
    return (id + " - " + name + " - " + field);
};
  
console.log(displayData(1, "Aman", "CSE"));

Output: The output of both the code snippet would remain the same which is thus illustrated below.

1 - Aman - CSE

Article Tags :