TypeScript Tutorial
TypeScript is a strongly-typed superset of JavaScript that compiles to plain JavaScript. It offers optional static typing, interfaces, and other features to improve the developer experience and make large-scale applications more manageable.
Installing TypeScriptβ
To get started with TypeScript, you need to install it globally using npm.
Step 1: Install Node.js and npmβ
If you donβt have Node.js and npm installed, visit the official Node.js website to download and install them.
Step 2: Install TypeScriptβ
Run the following command to install TypeScript globally on your system:
npm install -g typescript
Setting Up a TypeScript Projectβ
Step 1: Initialize the Projectβ
Create a new directory for your project and initialize it with npm:
mkdir my-typescript-project
cd my-typescript-project
npm init -y
Step 2: Create a tsconfig.json Fileβ
You can generate a tsconfig.json file to configure the TypeScript compiler:
tsc --init
This will generate a tsconfig.json file where you can adjust compiler options.
TypeScript Typesβ
TypeScript supports many types including:
stringnumberbooleanarrayobjectany
Example: Using Typesβ
let name: string = "Alice";
let age: number = 30;
let isActive: boolean = true;
Array Typesβ
let fruits: string[] = ["apple", "banana", "cherry"];
let numbers: Array<number> = [1, 2, 3];
any Typeβ
let value: any = "Hello";
value = 10; // This is allowed with the `any` type
TypeScript Functionsβ
In TypeScript, you can define functions with type annotations for both parameters and the return value.
function greet(name: string): string {
return "Hello, " + name;
}
Function with Optional Parameterβ
function greet(name: string, age?: number): string {
return age ? `Hello, ${name}, Age: ${age}` : `Hello, ${name}`;
}
Working with Classesβ
TypeScript adds features like access modifiers and type annotations to JavaScript classes.
Example: Defining a Classβ
class Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): string {
return `Hello, my name is ${this.name} and I am ${this.age} years old.`;
}
}
let person = new Person("Alice", 30);
console.log(person.greet());
Modules and Namespacesβ
TypeScript allows you to use modules and namespaces to organize your code.
Importing and Exporting Modulesβ
// greet.ts
export function greet(name: string): string {
return "Hello, " + name;
}
// app.ts
import { greet } from './greet';
console.log(greet("Alice"));
Compiling TypeScriptβ
To compile TypeScript files to JavaScript, use the tsc command:
tsc app.ts
This will generate an app.js file.
To compile all TypeScript files in your project, run:
tsc
Conclusionβ
TypeScript is a powerful language that helps catch errors early through static typing. It enhances JavaScript with object-oriented features and helps manage large-scale applications. With the installation and basic concepts covered in this tutorial, you can now start building TypeScript applications.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.