
Promise of T คือค่าที่จะมาในอนาคต async function คืน Promise เสมอ await แกะ Promise of T ออกมาเป็น T Promise.all คืน tuple ของผลลัพธ์ Promise.allSettled ไม่ล้มทั้งชุด และ catch ใน TypeScript สมัยใหม่จับ error เป็น unknown ต้อง narrow Read more »

any ปิด type check และแพร่กระจาย unknown บังคับให้ narrow ก่อนใช้ never คือ type ที่ไม่มีค่าใดเป็นไปได้ void คือไม่คืนค่า type assertion as บอก TypeScript ให้เชื่อโดยไม่ตรวจ satisfies เช็คว่าตรง type แต่ยังเก็บ type ที่แคบไว้ Read more »

strictNullChecks ทำให้ null และ undefined เป็น type แยกที่ต้องจัดการ optional chaining question-dot หยุดทันทีถ้าเจอ null nullish coalescing double-question ให้ค่าสำรองเฉพาะ null และ undefined non-null assertion exclamation บอก TypeScript ว่าไม่ null แต่ไม่ตรวจ runtime Read more »

user-defined type guard คือฟังก์ชันที่ return type เป็น x is SomeType บอก TypeScript ว่าถ้า return true แปลว่า x เป็น type นั้น assertion function ใช้ asserts x is T สำหรับกรณีที่ throw ถ้าไม่ตรง และ never... Read more »

TypeScript ใช้ ES module import export เหมือน JavaScript สมัยใหม่ named export กับ default export ต่างกันตรงไหน import type แยกการ import ที่ใช้แค่ตอน compile ออก และ path alias ทำให้เขียน import จากรากโปรเจกต์แทน relative path ยาว ๆ Read more »

decorator คือฟังก์ชันที่แนบพฤติกรรมพิเศษให้ class method หรือ field ด้วย at sign TypeScript 5.0 รองรับ TC39 decorators ที่เป็นมาตรฐาน ต่างจาก experimentalDecorators แบบเก่า บทนี้ว่าด้วยการเขียน decorator และ use case ที่พบบ่อยอย่าง logging กับ validation Read more »

get และ set ทำให้ property คำนวณหรือ validate ได้ static member เป็นของ class ไม่ใช่ instance hash private field เป็น private จริงตอน runtime ต่างจาก private ของ TypeScript ที่เป็นแค่ compile-time และ static block รัน initialization... Read more »

implements บอกว่า class ต้องมี member ครบตาม interface TypeScript ใช้ structural typing หรือ duck typing คือถ้า shape ตรงกันก็ใช้แทนกันได้ ไม่สนว่ามาจาก class ไหน ต่างจาก nominal typing ของ Java บทนี้อธิบายผลที่ตามมา Read more »

extends ให้คลาสลูกรับ field และ method จากคลาสแม่ ต้องเรียก super ใน constructor ก่อนใช้ this abstract class สร้าง instance เองไม่ได้ ใช้เป็นฐานร่วม abstract method บังคับให้ลูก implement และ override keyword กับ noImplicitOverride ช่วยกันพิมพ์ชื่อ method ผิด Read more »

class ใน TypeScript คือ class ของ JavaScript ที่เพิ่ม type ให้ field และ method access modifier public private protected คุมการเข้าถึง parameter properties ย่อการรับค่าเข้า field ใน constructor เหลือบรรทัดเดียว Read more »