Skip to content

การ Compile TypeScript

TypeScript ต้องถูก compile เป็น JavaScript ก่อนรัน โดยใช้คำสั่ง:

bash
tsc

ตัวเลือกการ compile

  • tsc --watch - compile อัตโนมัติเมื่อไฟล์เปลี่ยน
  • tsc --project tsconfig.json - ใช้ไฟล์ config ที่ระบุ
  • tsc --noEmitOnError - ไม่ compile ถ้ามี error

กระบวนการ compile

  1. อ่านไฟล์ .ts และ .d.ts
  2. ตรวจสอบ type
  3. สร้างไฟล์ .js และ .d.ts (ถ้าเปิดการสร้าง)
  4. รายงาน error (ถ้ามี)

เครื่องมือที่เกี่ยวข้อง

  • ts-node - รัน TypeScript โดยไม่ต้อง compile
  • swc - compiler ที่เร็วขึ้น
  • esbuild - bundler ที่ compile TypeScript ได้