Lesson 7 of 60 – Your First TypeScript Program
12%

Your First TypeScript Program

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.

Note: TypeScript source files normally use the .ts extension. The TypeScript compiler checks the code and can generate JavaScript that can be executed by a JavaScript runtime.

1. Create a Project Folder

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.

2. Open the Project Folder

Open the terminal and move into your project folder.

cd typescript-first-program

The cd command changes the current directory.

3. Create a TypeScript File

Create a file named:

app.ts

The .ts extension indicates that the file contains TypeScript code.

4. Write Your First 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.

5. Understanding the let Keyword

The let keyword is used to declare a variable.

let message: string = "Hello TypeScript";

Here, message is the name of the variable.

6. Understanding the string Type

The string type tells TypeScript that the variable should contain text.

let message: string = "Hello TypeScript";

The value "Hello TypeScript" is a string.

7. Understanding console.log()

The console.log() method displays information in the console.

console.log(message);

When the JavaScript program runs, it displays:

Hello TypeScript

8. Install TypeScript in the Project

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.

9. Compile the Program

After writing the program, compile it using:

npx tsc app.ts

The TypeScript compiler processes app.ts and generates JavaScript output.

10. Generated JavaScript File

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);

11. Run the JavaScript Program

If the generated JavaScript program is intended to run with Node.js, use:

node app.js

The output will be:

Hello TypeScript

12. Complete First Program

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().

13. Create a Number Variable

You can also create a number variable.

let age: number = 20;

console.log(age);

The number type indicates that age should contain a number.

14. Create a Boolean Variable

TypeScript also supports the boolean type.

let isStudent: boolean = true;

console.log(isStudent);

A boolean value can be true or false.

15. Multiple Variables

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);

16. Using Arithmetic Operations

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

17. Creating a Function

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"));

18. Function Parameter Type

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.

19. Function Return Type

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.

20. TypeScript Detects Incorrect Types

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.

21. Type Inference

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.

22. First Program with Type Inference

let course = "TypeScript";
let duration = 6;

console.log(course);
console.log(duration);

Here, TypeScript can infer the types from the assigned values.

23. Using const

The const keyword can also be used in TypeScript.

const language: string = "TypeScript";

console.log(language);

A value declared with const cannot be reassigned.

24. First Student Program

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.

25. First Calculation Program

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.

26. First TypeScript Program with an Object

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.

27. Recommended Project Structure

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.

28. TypeScript Program Flow

Write app.ts
     ↓
TypeScript Code
     ↓
Run tsc
     ↓
Type Checking
     ↓
Generate JavaScript
     ↓
Run JavaScript

This is the basic development flow you should remember.

29. Complete First TypeScript Example

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.

30. Your First TypeScript Workflow

The complete beginner workflow is:

  1. Create a project folder.
  2. Create an .ts file.
  3. Write TypeScript code.
  4. Install TypeScript if required.
  5. Compile the TypeScript file.
  6. Check the generated JavaScript.
  7. Run the JavaScript program.
app.ts
   ↓
npx tsc app.ts
   ↓
app.js
   ↓
node app.js
Remember: Your first TypeScript program can be very simple. Start with variables, types, functions, and console output. Later, you will use these concepts to build larger applications and React Native projects.

📌 Key Points

  • TypeScript files normally use the .ts extension.
  • let and const can be used to declare variables.
  • Type annotations specify expected data types.
  • string, number, and boolean are basic types.
  • console.log() displays information in the console.
  • Functions can have typed parameters and return types.
  • TypeScript can detect incorrect type assignments.
  • npx tsc app.ts can compile a TypeScript file.
  • The compiler can generate JavaScript output.
  • The same TypeScript concepts will be useful when learning React Native.

🧠 Quick Quiz

Question: Which file extension is normally used for a TypeScript file?