TypeScript Type System
TypeScript Type System
Phần tiêu đề “TypeScript Type System”Hệ thống types mạnh mẽ của TypeScript giúp code an toàn và dễ maintain hơn.
Type Inference
Phần tiêu đề “Type Inference”TypeScript tự suy luận types:
let x = 3; // numberlet name = "Phi"; // stringlet isActive = true; // boolean
const user = { name: "Phi", age: 25,}; // { name: string; age: number }Type Assertions
Phần tiêu đề “Type Assertions”const input = document.getElementById("input") as HTMLInputElement;const value = input.value;
// Hoặcconst input2 = <HTMLInputElement>document.getElementById("input");Literal Types
Phần tiêu đề “Literal Types”type Direction = "north" | "south" | "east" | "west";type HttpMethod = "GET" | "POST" | "PUT" | "DELETE";
let method: HttpMethod = "GET"; // OK// method = "PATCH"; // ErrorUtility Types
Phần tiêu đề “Utility Types”interface User { id: number; name: string; email: string; password: string;}
// Partial - Tất cả optionaltype PartialUser = Partial<User>;
// Required - Tất cả requiredtype RequiredUser = Required<User>;
// Pick - Chọn propertiestype UserPreview = Pick<User, "id" | "name">;
// Omit - Loại bỏ propertiestype UserWithoutPassword = Omit<User, "password">;
// Readonlytype ReadonlyUser = Readonly<User>;
// Recordtype Roles = Record<string, string[]>;Learn more
Phần tiêu đề “Learn more”Chi tiết hơn tại Advanced TypeScript