Loading content…
Loading content…
TypeScript adds static type checking to JavaScript. In production codebases, TypeScript catches bugs before runtime and makes code more maintainable.
// Primitives
const name: string = "Alice";
const age: number = 25;
const active: boolean = true;
// Arrays
const numbers: number[] = [1, 2, 3];
const strings: Array<string> = ["a", "b"];
// Union types
const id: string | number = 123;
// Any (avoid when possible)
let data: any = "anything";
// Interface
interface User {
name: string;
age: number;
email?: string; // Optional
}
// Type alias
type UserRole = "admin" | "user" | "guest";
// Using them
const user: User = {
name: "Alice",
age: 25
};
const role: UserRole = "admin";
// Function with types
const add = (a: number, b: number): number => {
return a + b;
};
// Function with optional parameters
const greet = (name: string, greeting: string = "Hello"): string => {
return `${greeting}, ${name}`;
};
Senior Developer Wisdom
Common Pitfall
any defeats the purpose of TypeScript. If you're unsure, use unknown and narrow it explicitly.TypeScript Fundamentals Checklist
Marking it complete updates your roadmap progress percentage.