How TypeScript Compilation Works? (original) (raw)

Last Updated : 18 Mar, 2025

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 features like type annotations are removed, leaving behind clean JavaScript code.

**Let's understand how TypeScript compilation works:

TypeScript Compilation Working

Steps of TypeScript Compilation

The TypeScript compilation process transforms your TypeScript code into executable JavaScript. Here’s how it works step by step:

**Example

Here we are going to see a TypeScript code snippet and its corresponding compiled JavaScript output. This will help you understand how TypeScript's type annotations and features are translated into standard JavaScript.

**TypeScript Code:

JavaScript `

let displayData = ( id: number, name: string, field: string) : string => { return (id + " - " + name + " - " + field); }

console.log(displayData(1 , "Aman", "CSE"));

`

**Compiled JavaScript Code:

After compiling the TypeScript code, the resulting JavaScript code is:

JavaScript `

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

`

**Output:

1 - Aman - CSE

Basic Facts About TypeScript

Here are some basic facts about TypeScript:

TypeScript Compilation Flaws

While TypeScript offers significant advantages, it's important to be aware of its limitations, particularly concerning compilation: