Basic Types
TypeScript เพิ่ม Type System เข้ามาใน JavaScript เพื่อช่วยตรวจจับ bugs ตั้งแต่ตอนเขียนโค้ด ไม่ต้องรอจนกว่าจะ run แล้วพัง
ทำไมต้องใช้ Types
Section titled “ทำไมต้องใช้ Types”| ข้อดี | คำอธิบาย |
|---|---|
| Catch Errors Early | เจอ bug ตั้งแต่ตอนเขียน ไม่ใช่ตอน production |
| Better IDE Support | Autocomplete, refactoring ทำงานได้ดีกว่า |
| Self-documenting | อ่านโค้ดแล้วรู้เลยว่าตัวแปรเก็บอะไร |
| Safer Refactoring | เปลี่ยนโค้ดแล้วรู้ทันทีว่าตรงไหนพัง |
Type System Overview
Section titled “Type System Overview”Static Type-Checking
Section titled “Static Type-Checking”TypeScript ทำ static type-checking ตอน compile ไม่ใช่ตอน runtime:
// JavaScript พัง runtimeconst message = "Hello!";message(); // TypeError: message is not a function
// TypeScript เจอตอน compileconst message: string = "Hello!";// message(); // Error: This expression is not callable.เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google