1๏ธโฃ TypeScript Syntax vs. JavaScript Syntax
TypeScript extends JavaScript, meaning you can write regular JavaScript code and add extra TypeScript features.
โ JavaScript Code (Valid in TypeScript)
let message = "Hello, World!";
console.log(message);
โ TypeScript Code with Type Annotations
let message: string = "Hello, World!";
console.log(message);
๐ Key difference: The : string specifies that message must always be a string.
2๏ธโฃ Variables and Data Types in TypeScript
In TypeScript, you must define data types for variables.
๐น Declaring Variables with Type Annotations
let name: string = "John"; // String type
let age: number = 25; // Number type
let isStudent: boolean = true; // Boolean type
โ TypeScript enforces the correct data type, preventing unexpected errors.
๐น Using let, const and var
| Keyword | Scope | Reassignment Allowed? |
|---|---|---|
| let | Block | โ Yes |
| const | Block | โ No |
| var | Function | โ Yes (Avoid using it) |
Example:
let username: string = "Alice";
const PI: number = 3.14;
var country: string = "India"; // Not recommended in TypeScript
3๏ธโฃ Functions in TypeScript
TypeScript functions must define input and output types.
๐น Function with Parameters and Return Type
function addNumbers(a: number, b: number): number {
return a + b;
}
console.log(addNumbers(10, 20)); // Output: 30
โ
Explanation:
โ a: number, b: number โ Both parameters must be numbers.
โ : number โ The function returns a number.
๐น Function with Optional Parameters
Use ? to make parameters optional.
function greet(name: string, age?: number): string {
if (age) {
return `Hello, ${name}. You are ${age} years old.`;
}
return `Hello, ${name}.`;
}
console.log(greet("John")); // Output: Hello, John.
console.log(greet("John", 25)); // Output: Hello, John. You are 25 years old.
4๏ธโฃ TypeScript Interfaces
An interface defines the structure of an object.
๐น Defining and Using Interfaces
interface Person {
name: string;
age: number;
isStudent?: boolean; // Optional property
}
let user: Person = { name: "Alice", age: 22 };
console.log(user.name); // Output: Alice
โ
Why Use Interfaces?
โ Ensures objects follow a specific structure.
โ Helps in better code organization and error prevention.
5๏ธโฃ TypeScript Classes
Classes in TypeScript use object-oriented programming (OOP) principles like encapsulation, inheritance, and polymorphism.
๐น Defining a Class
class Car {
brand: string;
model: string;
constructor(brand: string, model: string) {
this.brand = brand;
this.model = model;
}
getDetails(): string {
return `Car: ${this.brand} ${this.model}`;
}
}
let myCar = new Car("Toyota", "Corolla");
console.log(myCar.getDetails()); // Output: Car: Toyota Corolla
โ
Key Features:
โ constructor() initializes object properties.
โ getDetails() is a method that returns car details.
6๏ธโฃ TypeScript Arrays and Tuples
Arrays and tuples store multiple values.
๐น Array Declaration
let fruits: string[] = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // Output: Apple
๐น Tuple Declaration
Tuples allow fixed types at specific positions.
let person: [string, number] = ["Alice", 25];
console.log(person[0]); // Output: Alice
โ
Difference:
โ Array โ Stores multiple values of the same type.
โ Tuple โ Stores different types in a specific order.
7๏ธโฃ TypeScript Enums
Enums are used to define a set of named constants.
enum Direction {
Up = 1,
Down,
Left,
Right
}
let move: Direction = Direction.Up;
console.log(move); // Output: 1
โ Enums help in better readability when dealing with fixed values.
8๏ธโฃ TypeScript Union and Intersection Types
Union (|) allows multiple types, and Intersection (&) combines types.
๐น Union Type (Variable can hold multiple types)
let id: number | string;
id = 101; // Valid
id = "AB101"; // Also valid
๐น Intersection Type (Combining Interfaces)
interface A {
propA: string;
}
interface B {
propB: number;
}
type Combined = A & B;
let obj: Combined = { propA: "Hello", propB: 100 };
โ
Why Use These?
โ Union types allow flexibility.
โ Intersection types combine multiple interfaces for more control.
9๏ธโฃ TypeScript Type Aliases
Aliases provide custom names for complex types.
type ID = number | string;
let userId: ID = 123; // Valid
userId = "ABC"; // Also valid
โ Aliases make code more readable and reusable.
๐ TypeScript Generics
Generics allow reusable code without specifying exact data types.
function identity<T>(value: T): T {
return value;
}
console.log(identity<string>("Hello")); // Output: Hello
console.log(identity<number>(100)); // Output: 100
โ Generics improve code reusability by allowing different data types.

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.