TypeScript Compilation
Learn how to compile TypeScript code into JavaScript, understand the generated file, and execute your program using Node.js.
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
.tsfile. - 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.
TypeScript Compilation Workflow
Follow this diagram to understand how a TypeScript program moves from source code to execution.
Step 1
Write CodeCreate the app.ts file.
Step 2
CompileRun the tsc compiler.
Step 3
Generate JavaScriptProduce app.js.
Step 4
Run ProgramExecute using Node.js.
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 |
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
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.
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);
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
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.
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.
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.jsbefore the JavaScript file has been generated. - Ignoring TypeScript compiler errors.
- Looking for the generated JavaScript in the wrong directory.
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