TypeScript
TypeScript
Phần tiêu đề “TypeScript”TypeScript là superset của JavaScript, thêm static type checking và các tính năng nâng cao.
Tại sao dùng TypeScript?
Phần tiêu đề “Tại sao dùng TypeScript?”✅ Catch errors sớm - Lỗi type được phát hiện lúc compile
✅ Better IDE support - Autocomplete, refactoring
✅ Self-documenting - Types là documentation
✅ Safer refactoring - Thay đổi code tự tin hơn
✅ Better for large codebases - Scale tốt hơn JavaScript
Setup
Phần tiêu đề “Setup”npm install -D typescriptnpx tsc --initBasic Types
Phần tiêu đề “Basic Types”// Primitiveslet name: string = "Phi";let age: number = 25;let isActive: boolean = true;
// Arrayslet numbers: number[] = [1, 2, 3];let names: Array<string> = ["Phi", "Anh"];
// Tuplelet tuple: [string, number] = ["Phi", 25];
// Enumenum Role { Admin = "ADMIN", User = "USER", Guest = "GUEST",}
// Any (tránh dùng!)let anything: any = "can be anything";
// Unknown (an toàn hơn any)let userInput: unknown;userInput = 5;userInput = "hello";
// Voidfunction log(): void { console.log("No return value");}
// Neverfunction throwError(): never { throw new Error("Error!");}Interfaces
Phần tiêu đề “Interfaces”interface User { id: number; name: string; email: string; age?: number; // Optional readonly createdAt: Date; // Readonly}
const user: User = { id: 1, name: "Phi", email: "phi@example.com", createdAt: new Date(),};
// user.createdAt = new Date(); // Error: readonlyType Aliases
Phần tiêu đề “Type Aliases”type ID = string | number;type User = { id: ID; name: string;};
type Admin = User & { role: "admin"; permissions: string[];};Union và Intersection Types
Phần tiêu đề “Union và Intersection Types”// Union - một trong các typestype Status = "pending" | "approved" | "rejected";let orderStatus: Status = "pending";
function printId(id: string | number) { if (typeof id === "string") { console.log(id.toUpperCase()); } else { console.log(id); }}
// Intersection - Kết hợp typestype Person = { name: string;};
type Employee = Person & { employeeId: number;};Generics
Phần tiêu đề “Generics”// Generic functionfunction identity<T>(arg: T): T { return arg;}
const num = identity<number>(5);const str = identity<string>("hello");
// Generic interfaceinterface Response<T> { data: T; status: number; message: string;}
type UserResponse = Response<User>;type UsersResponse = Response<User[]>;
// Generic constraintsinterface HasLength { length: number;}
function logLength<T extends HasLength>(arg: T): void { console.log(arg.length);}Classes
Phần tiêu đề “Classes”class Animal { protected name: string;
constructor(name: string) { this.name = name; }
move(distance: number): void { console.log(`${this.name} moved ${distance}m`); }}
class Dog extends Animal { bark(): void { console.log("Woof!"); }}
const dog = new Dog("Buddy");dog.bark();dog.move(10);