TypeScript je JavaScript s typy. A to mění všechno.
Proč TypeScript¶
- Chyby zachytíte při kompilaci, ne v produkci
- Lepší IDE podpora (autocomplete, refactoring)
- Dokumentace v kódu
- Bezpečnější refactoring
Základní typy¶
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
return arr[0];
}
interface ApiResponse
data: T;
error?: string;
}
Utility Types¶
Partial
Required
Pick
Omit
Record
Enums vs Union Types¶
// Preferujte union types
type Direction = “north” | “south” | “east” | “west”;
// Enum jen když potřebujete runtime hodnoty
enum HttpStatus { OK = 200, NotFound = 404 }
Strict mode¶
// tsconfig.json
{ “compilerOptions”: { “strict”: true } }
// Vždy! Bez strict mode ztrácíte polovinu výhod TS.
Pravidlo¶
Strict mode vždy zapnutý. Vyhněte se “any”. Pokud nevíte typ, použijte “unknown”.