TypeScript Tutorial

TypeScript กับ Node.js

at-types slash node ให้ type ของ Node API process fs path เป็นต้น process.env ทุก key เป็น string หรือ undefined ต้อง validate ด้วย zod ตอน startup ใน ESM ไม่มี __dirname ใช้ import.meta.dirname แทน... Read more »
TypeScript Tutorial

TypeScript ESLint และ Prettier

typescript-eslint คือ ESLint สำหรับ TypeScript type-aware rules ต้องเชื่อมกับ tsconfig เพื่อดู type ตอน lint จับบั๊กอย่าง floating promise และ unsafe any Prettier จัดรูปแบบโค้ดอัตโนมัติ แยกหน้าที่กันชัดเจน ESLint จับบั๊ก Prettier จัดสวย Read more »
TypeScript Tutorial

TypeScript เขียนเทสต์

Vitest รัน TypeScript ได้ทันทีไม่ต้องตั้งค่าเยอะ typing test ด้วย describe it expect การ mock module และ function ที่ยังคง type และ type-level test ด้วย expectTypeOf กับ ts-expect-error เพื่อทดสอบว่า type ทำงานถูกต้อง Read more »
TypeScript Tutorial

TypeScript สร้างและ publish library

publish library TypeScript ต้อง build เป็น JavaScript พร้อม declaration file package.json ต้องมี exports ที่ชี้ไฟล์ ESM CJS และ types อย่างถูกต้อง tsup ช่วย build dual format ได้ในคำสั่งเดียว และ files field คุมว่าอะไรถูก publish Read more »
TypeScript Tutorial

TypeScript declaration file และ @types

declaration file นามสกุล d.ts มีแต่ type ไม่มี implementation library JavaScript ที่ไม่มี type ในตัวติดตั้ง type จาก DefinitelyTyped ด้วย at-types slash ชื่อ declare สร้าง ambient declaration สำหรับ global หรือ module และเราเขียน d.ts เองได้เมื่อ library... Read more »
TypeScript Tutorial

TypeScript tsconfig.json เจาะลึก

tsconfig.json คุมทุกอย่างของ TypeScript strict เป็นชุดของ flag ย่อยที่ควรเปิดทั้งหมด target module lib กำหนดว่า compile เป็น JS เวอร์ชันไหนและมี API อะไร moduleResolution bundler สำหรับโปรเจกต์ที่ใช้ bundler และ flag เพิ่มความเข้มอย่าง noUncheckedIndexedAccess Read more »
TypeScript Tutorial

TypeScript ข้อมูลจากภายนอกและ runtime validation

JSON.parse คืน any ทำให้ type check หลุด type ใน TypeScript หายตอน compile ไม่มีการตรวจ runtime ข้อมูลจาก API form localStorage อาจไม่ตรง type ที่ประกาศ ต้อง validate ตอน runtime ด้วย type guard หรือ zod ที่ทั้ง validate... Read more »
TypeScript Tutorial

TypeScript ทำงานกับ DOM และ event

ฝั่งเบราว์เซอร์เปิด lib dom ใน tsconfig เพื่อได้ type ของ document window HTMLElement querySelector รับ generic บอก element type ที่คาดหวัง event listener มี type ตามชนิด event MouseEvent KeyboardEvent และการ cast EventTarget เป็น element ที่ถูก Read more »
TypeScript Tutorial

TypeScript array method และ iteration ให้ type ถูก

array method อย่าง map filter reduce มี type ที่ TypeScript infer ให้จาก callback reduce มักต้องระบุ type ของ accumulator filter narrow ได้ด้วย type guard Object.keys คืน string เสมอ Object.entries typing ที่ต้องระวัง และ for-of... Read more »
TypeScript Tutorial

TypeScript จัดการข้อผิดพลาด

สร้าง custom error ด้วยการ extends Error narrow error ใน catch ด้วย instanceof การเพิ่ม field ให้ error class การตั้ง name และ cause และ Result type pattern ที่คืน error เป็นค่าแทนการ throw ทำให้ compiler บังคับให้จัดการทั้งกรณีสำเร็จและล้มเหลว Read more »