Click on a topic to expand and learn in detail.
let message: string = "Hello, TypeScript!";
console.log(message);
TypeScript is a superset of JavaScript with static typing, making code more robust.
let name: string = "Alice";
let age: number = 25;
let isStudent: boolean = true;
let scores: number[] = [90, 85, 78];
TypeScript allows defining types for variables, arrays, objects, etc.
function add(a: number, b: number): number {
return a + b;
}
console.log(add(5, 3));
Functions in TypeScript have typed parameters and return types.
interface Person {
name: string;
age: number;
}
let p: Person = { name: "Alice", age: 25 };
console.log(p.name);
Interfaces define the structure of objects for type safety.
class Student {
name: string;
age: number;
constructor(name: string, age: number){
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello ${this.name}`);
}
}
let s = new Student("Alice", 25);
s.greet();
TypeScript supports full object-oriented programming with classes and inheritance.
function identity(arg: T): T { return arg; } console.log(identity (5)); console.log(identity ("Hello"));
Generics allow writing reusable, type-safe functions and classes.
// Enum example
enum Color { Red, Green, Blue }
let c: Color = Color.Green;
// Union type
let val: string | number = "Hello";
val = 42;