Junior #types
interface và type khác nhau thế nào?
⚡ Trả lời nhanh
Cả hai mô tả hình dạng object. interface có thể mở rộng/khai báo gộp (declaration merging); type linh hoạt hơn: hỗ trợ union, tuple, mapped/conditional type.
interface User { id: number }
interface User { name: string } // gộp: User có id + name
type Id = string | number; // union: chỉ type làm được
type Pair = [string, number]; // tuple
type Admin = User & { role: 'admin' }; // intersection