Now that you understand TypeScript and the TypeScript compiler, it is time to create your first TypeScript program. In this lesson, you will learn how to create a .ts file, write TypeScript code, compile it, and understand the generated JavaScript.
.ts extension.
The TypeScript compiler checks the code and can generate JavaScript
that can be executed by a JavaScript runtime.
First, create a folder for your TypeScript program. For example:
typescript-first-program
You can create this folder anywhere you normally keep your programming projects.
Open the terminal and move into your project folder.
cd typescript-first-program
The cd command changes the current directory.
Create a file named:
app.ts
The .ts extension indicates that the file contains
TypeScript code.
Write the following code inside app.ts:
let message: string = "Hello TypeScript";
console.log(message);
This program creates a string variable and prints its value.
The let keyword is used to declare a variable.
let message: string = "Hello TypeScript";
Here, message is the name of the variable.
The string type tells TypeScript that the variable should
contain text.
let message: string = "Hello TypeScript";
The value "Hello TypeScript" is a string.
The console.log() method displays information in the
console.
console.log(message);
When the JavaScript program runs, it displays:
Hello TypeScript
If TypeScript is not already installed in the project, install it using npm:
npm install --save-dev typescript
This installs TypeScript as a development dependency.
After writing the program, compile it using:
npx tsc app.ts
The TypeScript compiler processes app.ts and generates
JavaScript output.
After compilation, you can get a JavaScript file named:
app.js
The TypeScript type annotation does not need to remain in the generated JavaScript.
let message = "Hello TypeScript";
console.log(message);
If the generated JavaScript program is intended to run with Node.js, use:
node app.js
The output will be:
Hello TypeScript
The complete TypeScript source code is:
let message: string = "Hello TypeScript";
console.log(message);
This is a simple TypeScript program using a variable, a type annotation,
and console.log().
You can also create a number variable.
let age: number = 20;
console.log(age);
The number type indicates that age should
contain a number.
TypeScript also supports the boolean type.
let isStudent: boolean = true;
console.log(isStudent);
A boolean value can be true or false.
You can create multiple typed variables in the same program.
let name: string = "Rahul";
let age: number = 21;
let student: boolean = true;
console.log(name);
console.log(age);
console.log(student);
TypeScript supports JavaScript arithmetic operations.
let a: number = 10;
let b: number = 20;
let total: number = a + b;
console.log(total);
The output is:
30
You can create functions in TypeScript just as you do in JavaScript, while also adding type information.
function greet(name: string): string {
return "Hello " + name;
}
console.log(greet("Rahul"));
The parameter name is declared as a string.
function greet(name: string) {
return "Hello " + name;
}
This tells TypeScript that the function expects a string argument.
You can also specify what type a function should return.
function add(
a: number,
b: number
): number {
return a + b;
}
The return type is number.
TypeScript can detect incorrect type assignments.
let age: number = 20;
age = "Twenty";
The second line causes a type error because a string is assigned to a number variable.
TypeScript can often determine the type automatically from the initial value.
let name = "Rahul";
let age = 20;
TypeScript can infer that name is a string and
age is a number.
let course = "TypeScript";
let duration = 6;
console.log(course);
console.log(duration);
Here, TypeScript can infer the types from the assigned values.
The const keyword can also be used in TypeScript.
const language: string = "TypeScript";
console.log(language);
A value declared with const cannot be reassigned.
Let's create a small student example.
let studentName: string = "Amit";
let marks: number = 85;
console.log("Student:", studentName);
console.log("Marks:", marks);
This program stores and displays basic student information.
let price: number = 500;
let quantity: number = 3;
let total: number = price * quantity;
console.log("Total:", total);
The program calculates the total price by multiplying price and quantity.
let student: {
name: string;
age: number;
} = {
name: "Rahul",
age: 20
};
console.log(student.name);
console.log(student.age);
This example describes the expected structure of the object.
A simple TypeScript project can be organized like this:
typescript-first-program/
│
├── node_modules/
├── app.ts
├── app.js
├── package.json
├── package-lock.json
└── tsconfig.json
The exact structure can change depending on the project and build tools.
Write app.ts
↓
TypeScript Code
↓
Run tsc
↓
Type Checking
↓
Generate JavaScript
↓
Run JavaScript
This is the basic development flow you should remember.
let studentName: string = "Rahul";
let age: number = 21;
let marks: number = 88;
function showStudent(
name: string,
age: number,
marks: number
): void {
console.log("Name:", name);
console.log("Age:", age);
console.log("Marks:", marks);
}
showStudent(
studentName,
age,
marks
);
This example combines variables, type annotations, a function, parameters, and console output.
The complete beginner workflow is:
.ts file.app.ts
↓
npx tsc app.ts
↓
app.js
↓
node app.js
.ts extension.let and const can be used to declare variables.string, number, and boolean are basic types.console.log() displays information in the console.npx tsc app.ts can compile a TypeScript file.Question: Which file extension is normally used for a TypeScript file?