TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It adds optional static typing, interfaces, and other features that make large codebases much more maintainable.
Basic Types
let name: string = 'Alice';
let age: number = 30;
let isActive: boolean = true;
let tags: string[] = ['ts', 'js'];Interfaces
interface Post {
id: number;
title: string;
content: string;
published?: boolean; // optional
}Functions
function greet(name: string): string {
return `Hello, ${name}!`;
}
const add = (a: number, b: number): number => a + b;Generics
function first<T>(arr: T[]): T {
return arr[0];
}
const num = first([1, 2, 3]); // type: number
const str = first(['a', 'b']); // type: string