Welcome to the TypeScript Tutorials section of the Project_Nova_Website! TypeScript is a superset of JavaScript that adds static types, and it's a great way to improve the quality of your JavaScript code. Below, you will find a series of tutorials to help you get started with TypeScript.
Getting Started
Before diving into the tutorials, make sure you have Node.js and npm installed. You can download and install Node.js from here.
Install TypeScript
To install TypeScript, open your terminal and run:
npm install -g typescript
Hello World
Your first TypeScript program can be as simple as this:
function greet(name: string) {
return "Hello, " + name + "!";
}
console.log(greet("World"));
To compile and run this code, save it as hello.ts
and execute:
tsc hello.ts
node hello.js
You should see the output:
Hello, World!
Basic Types
TypeScript supports several basic types, such as string
, number
, boolean
, and any
.
String
let message: string = "Hello, TypeScript!";
console.log(message);
Number
let age: number = 30;
console.log(age);
Boolean
let isTrue: boolean = true;
console.log(isTrue);
Any
Use any
when you are not sure about the type of a variable.
let mystery: any = 42;
mystery = "I am a string now!";
console.log(mystery);
Advanced Types
TypeScript also supports advanced types like arrays, tuples, enums, and interfaces.
Arrays
let numbers: number[] = [1, 2, 3];
console.log(numbers);
Tuples
let person: [string, number] = ["Alice", 25];
console.log(person);
Enums
enum Color {
Red,
Green,
Blue
}
let favoriteColor = Color.Green;
console.log(favoriteColor);
Interfaces
interface Person {
name: string;
age: number;
}
let user: Person = {
name: "Bob",
age: 25
};
console.log(user);
Further Reading
To learn more about TypeScript, check out the official TypeScript documentation.
[center]
[/center]
Enjoy your TypeScript journey!