The Complete Guide to TypeScript¶
TypeScript is JavaScript with types. And that changes everything.
Why TypeScript¶
- Catch errors at compile time, not in production
- Better IDE support (autocomplete, refactoring)
- Documentation in code
- Safer refactoring
Basic Types¶
let name: string = “TypeScript”; let version: number = 5.3; let active: boolean = true; let items: string[] = [“a”, “b”]; let tuple: [string, number] = [“age”, 30];
Interfaces & Types¶
interface User { id: number; name: string; email?: string; // optional readonly createdAt: Date; }
type Status = “active” | “inactive” | “banned”;
Generics¶
function first
interface ApiResponse
Utility Types¶
Partial
Enums vs Union Types¶
// Prefer union types type Direction = “north” | “south” | “east” | “west”;
// Enum only when you need runtime values enum HttpStatus { OK = 200, NotFound = 404 }
Strict Mode¶
// tsconfig.json { “compilerOptions”: { “strict”: true } } // Always! Without strict mode you lose half the benefits of TS.
Rule¶
Strict mode always on. Avoid “any”. If you do not know the type, use “unknown”.