Master TypeScript From Scratch

Learn TypeScript step by step with practical examples, interactive coding lessons, and real-world projects.

TypeScript Compilation

Learn how to compile TypeScript code into JavaScript, understand the generated file, and execute your program using Node.js.

Simple Explanation: Compilation is the process of converting TypeScript code into JavaScript code. The TypeScript compiler is called tsc.

What Is TypeScript Compilation?

TypeScript files use the .ts extension. JavaScript runtimes execute JavaScript code, so TypeScript is commonly compiled into JavaScript before execution.

  • Write the source code in a .ts file.
  • Compile the source file using the TypeScript compiler.
  • Generate the corresponding JavaScript file.
  • Execute the generated JavaScript using Node.js or another suitable JavaScript runtime.
Remember: TypeScript compilation removes type annotations from the generated JavaScript and performs the checks configured for the project.

TypeScript Compilation Workflow

Follow this diagram to understand how a TypeScript program moves from source code to execution.

📝
Step 1
Write Code

Create the app.ts file.

→
⚙️
Step 2
Compile

Run the tsc compiler.

→
📄
Step 3
Generate JavaScript

Produce app.js.

→
🚀
Step 4
Run Program

Execute using Node.js.

Complete Workflow: app.ts → npx tsc app.ts → app.js → node app.js

1. Requirements for Compilation

Before compiling TypeScript, make sure the required tools are available on your computer.

Tool Purpose Verification Command
Node.js Runs JavaScript outside the browser. node -v
npm Installs and manages project packages. npm -v
TypeScript Checks and compiles TypeScript code. npx tsc --version
Note: If TypeScript is installed locally in your project, use npx tsc to run its compiler.

2. Create a TypeScript File

Open your project folder in Visual Studio Code and create a new file called app.ts.

File Name

app.ts

Write the following TypeScript code inside this file.

Example

let studentName: string = "Rahul";
let age: number = 22;
let courseName: string = "TypeScript";

console.log("Student Name: " + studentName);
console.log("Age: " + age);
console.log("Course: " + courseName);

Expected Output

Student Name: Rahul
Age: 22
Course: TypeScript

This example declares three variables and displays their values using console.log().

3. Understand the TypeScript Code

Code Explanation
let studentName Declares a variable to store the student's name.
: string Specifies the variable's string type.
: number Specifies the variable's numeric type.
console.log() Displays information in the console or terminal.
app.ts Contains the original TypeScript source code.

4. Install TypeScript

If TypeScript is not installed in your project, open the terminal in your project folder and run these commands.

Step 1: Initialize npm

npm init -y

Step 2: Install TypeScript

npm install --save-dev typescript

Step 3: Verify Installation

npx tsc --version
Expected Result: The terminal displays the installed TypeScript compiler version.

5. Compile TypeScript Code

Open the terminal in the folder containing app.ts. Run the following command.

Compilation Command

npx tsc app.ts

The TypeScript compiler checks the file and generates JavaScript according to the compiler settings.

Generated File

app.ts
app.js

Here, app.ts is your source file and app.js is the generated JavaScript file.

Remember: The generated file may be placed in a different directory when compiler options specify an output location.

6. Understand the Generated JavaScript

TypeScript type annotations are removed from the generated JavaScript. For the example above, the output can look like this.

Generated app.js

var studentName = "Rahul";
var age = 22;
var courseName = "TypeScript";

console.log("Student Name: " + studentName);
console.log("Age: " + age);
console.log("Course: " + courseName);
Note: The exact JavaScript syntax depends on your TypeScript compiler version and target configuration. The example above illustrates the result for a compatible target.

7. Run the Generated JavaScript

After compilation, execute the generated JavaScript file using Node.js.

Command

node app.js

Expected Output

Student Name: Rahul
Age: 22
Course: TypeScript
Congratulations! You have written TypeScript, compiled the program into JavaScript, and executed the generated file.

8. What Is Watch Mode?

Watch mode checks your TypeScript file again when you make changes and save the file.

Command

npx tsc app.ts --watch

This helps developers avoid manually starting the compiler after every change.

Important: Watch mode recompiles TypeScript. It does not automatically execute the generated JavaScript using Node.js.

9. Understanding tsconfig.json

A tsconfig.json file defines compiler settings for a TypeScript project. It is useful when a project contains multiple TypeScript files.

Create Configuration

npx tsc --init

Compile the Project

npx tsc

When a tsconfig.json file exists, npx tsc without a source file argument compiles the project using its configuration.

Remember: Compiler options determine settings such as the JavaScript target, module system, type checking, and output directory.

Practical Example: CIIT Student Details

Let's compile a practical example that displays student details and the selected training course.

Example

interface Student {
    id: number;
    name: string;
    course: string;
}

const student: Student = {
    id: 101,
    name: "Priya",
    course: "Dot Net Full Stack"
};

console.log("Student ID: " + student.id);
console.log("Student Name: " + student.name);
console.log("Selected Course: " + student.course);

Expected Output

Student ID: 101
Student Name: Priya
Selected Course: Dot Net Full Stack

The Student interface describes the required properties of the object. The compiler checks the object against this structure.

Common Compilation Mistakes

  • Running the command from the wrong folder.
  • Forgetting to save the TypeScript file before compilation.
  • Running node app.js before the JavaScript file has been generated.
  • Ignoring TypeScript compiler errors.
  • Looking for the generated JavaScript in the wrong directory.
Troubleshooting Tip: Read the compiler error in the terminal first. Correct the reported issue and run the compilation command again.

Practice Task

Create a file named student.ts and write a program to display the following information.

  • Student Name: Amit
  • Course Name: Full Stack Development
  • Duration: 6 months

Declare the variables using the correct TypeScript types, compile the program, and run the generated JavaScript.

Hint

npx tsc student.ts
node student.js

Try completing this task yourself before checking the answer with your trainer.

Summary

In this lesson, we learned how to create a TypeScript file, compile it with tsc, understand the generated JavaScript, and execute it with Node.js. We also explored watch mode, tsconfig.json, and common compilation errors.

Key Workflow: app.ts → npx tsc app.ts → app.js → node app.js