- Install Node.js:
TypeScript requires Node.js. If you don't have it installed, download and install Node.js from the official website: https://nodejs.org/ - Install TypeScript using npm:
Once Node.js is installed, open your terminal or command prompt and run the following command to install TypeScript globally on your system:
npm install -g typescript
The-g
flag ensures that TypeScript is installed globally, making it accessible from any directory in your system. - Verify TypeScript Installation:
After the installation is complete, you can check the TypeScript version to ensure it was installed successfully. Run the following command:
tsc --version
This will display the version number of TypeScript installed on your system. - Create a TypeScript File:
Now that TypeScript is installed, let's create a simple TypeScript file. Create a new file with a.ts
extension, for example,app.ts
. - Write TypeScript Code:
Open theapp.ts
file in a code editor and add the following TypeScript code:
In this example, we have a functionfunction greet(name: string) { console.log(`Hello, ${name}!`); }
let personName: string = "Arham"; greet(personName);
greet
that takes aname
parameter of typestring
and logs a greeting to the console. - Compile TypeScript to JavaScript:
TypeScript code needs to be compiled to JavaScript before it can be executed in the browser or Node.js. Run the following command to compile theapp.ts
file:
tsc app.ts
This will create a new file calledapp.js
, which contains the JavaScript code generated from your TypeScript code. - Run the JavaScript Code:
You can now run the generated JavaScript code using Node.js:
node app.js
The output should be:
Hello, Arham!
Congratulations! You've successfully installed TypeScript and run your first TypeScript program.
It is a statically typed language.
We have to explicitly tell the type of data, for example:
let firstValue:number=5;
We can also give a type to one variable to another, for example:
let someVar=10;
let newVar:someVar=123;
This will check the type of data stored in someVar
and then assign that to newVar
.
The following example will take any type and convert it into the specified type:
let something:any=100; let anotherOne=something as number;
ORlet anotherOne=<number>something;
We can declare arrays with a specific type:
let arr:string[]=["hello","world","string_type"];
ORlet arr:Array<string>=["hello","world","string_type"];
Tuples hold multiple data just like arrays, but they can have different types of data:
let tup:[string,string,number]=["typescript","learning",10];
We can also make an array of tuples:
let arrOfTup:[number,string][]=[[5,"bannaa"],[10,"apples"]];
We can declare the inputs and specify the type of outputs needed as a return from the function:
function sum(firstNum:number,secNum:number):number{
return firstNum+secNum;
}
If we don't want to return anything from a function, we can use void
:
function noReturn(firstNum:number,secNum:number):void{
//this shouldn't return anything
}
This allows us to declare constants with numeric or string values:
enum bookCategories{
history, // by default it is given 0
numOfBooks, // by default it is given 1
numOfShelves // by default it is given 2
}
We can also specify the numbers in the enum:
enum meatStock{
chicken=100,
mutton=20,
beef // NOTICE THAT BEEF DOES NOT HAVE ANY VALUE, SO IT WILL OBTAIN THE NEXT INDEX OF THE PREVIOUSLY DEFINED VALUE, IN THIS CASE 21
}
We can also make combinations of strings and numbers in an enum without any problems.
This defines a specific structure for an object:
interface videoRequest{
id:number,
title:string,
comments:string,
likes:number,
dislikes?:number, // The ? indicates that it can either have dislikes or not
readonly username:string // This indicates that it can only be assigned when initialized
}