| name | typescript-utility-types |
| description | null |
Utility Types
Category: typescript · Status: 🟢 Active
When to use
Khi cần biến đổi type sẵn có thay vì khai báo lại từ đầu.
Steps
Pick/Omit để lấy/loại field từ type gốc, giữ liên kết khi gốc đổi.
Partial/Required cho update payload hoặc bắt buộc toàn bộ field.
Record<K, V> cho map/dictionary có key cố định.
ReturnType/Parameters để suy kiểu từ hàm có sẵn, tránh khai báo trùng.
- Compose nhiều utility nhưng giữ dễ đọc; đặt alias nếu type lồng sâu.
Template
interface User { id: string; name: string; email: string; role: Role; }
type UserPreview = Pick<User, "id" | "name">;
type UserUpdate = Partial<Omit<User, "id">>;
type UsersByRole = Record<Role, User[]>;
type ApiUser = ReturnType<typeof fetchUser>;
Example
Good: Partial<Omit<User, "id">> cho payload update; Record<Role, User[]> cho nhóm.
Avoid: Khai báo lại UserUpdate thủ công, lệch khi User thay đổi field.
Checklist