Type Branding
TypeScript ใช้ structural typing ซึ่งบางครั้งยอมให้ types ที่มี structure เหมือนกันใช้แทนกันได้ Type branding ช่วยสร้าง nominal types ที่แยกกันจริงๆ
ปัญหา Structural Typing
Section titled “ปัญหา Structural Typing”// ทั้งสองเป็น number - ใช้แทนกันได้!type UserId = number;type ProductId = number;
function getUser(id: UserId) {}getUser(productId); // ไม่ error!เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google