Type Guards
Type Guards คือเทคนิคที่ใช้ตรวจสอบ types ที่ runtime เพื่อให้ TypeScript narrow types ลงได้ ช่วยให้เข้าถึง properties และ methods ได้อย่างปลอดภัย
ประเภทของ Type Guards
Section titled “ประเภทของ Type Guards”| ประเภท | Syntax | ใช้กับ |
|---|---|---|
| typeof | typeof x === "string" | Primitives |
| instanceof | x instanceof Date | Classes |
| in | "prop" in obj | Object properties |
| Predicate | x is Type | Custom functions |
| Assertion | asserts x is Type | Throw if false |
Type Guard Flow
Section titled “Type Guard Flow”เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google