TypeScript ติดตั้งและตั้งค่าโปรเจกต์แรก
เตรียมเครื่อง
ต้องมี Node.js 20 ขึ้นไป (มาพร้อม npm):
node -v # v20.x หรือสูงกว่าสร้างโปรเจกต์
mkdir my-ts-app && cd my-ts-app
npm init -y
npm install --save-dev typescript tsx @types/nodetypescript— คอมไพเลอร์tsc+ type checkertsx— รันไฟล์.tsตรง ๆ ตอนพัฒนา (strip type + รัน, ไม่เก็บ output)@types/node— type ของ Node.js API (process,fs, ฯลฯ)
ติดตั้งเป็น dev dependency (--save-dev) — TypeScript ไม่ต้องมีบน production (compile แล้วได้ JS)
สร้าง tsconfig.json
npx tsc --initสร้างไฟล์ tsconfig.json ที่มี option เต็มไปหมด (comment ไว้) — สำหรับเริ่มต้น ใช้แบบกระชับนี้:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "dist",
"rootDir": "src",
"sourceMap": true
},
"include": ["src/**/*"]
}| option | ทำอะไร |
|---|---|
"strict": true | เปิดการตรวจเข้มทั้งหมด — ต้องมี (บทที่ 31 แตกเป็น flag ย่อย) |
"target" | เวอร์ชัน JS ที่ compile ออกมา |
"module" / "moduleResolution" | ระบบ module (NodeNext สำหรับ Node ESM, "bundler" เมื่อใช้ Vite/esbuild) |
"outDir" / "rootDir" | ที่เก็บ output / ที่มา source |
"skipLibCheck" | ข้ามการ check .d.ts ของ library (เร็วขึ้น) |
"sourceMap" | ให้ debugger map กลับไป .ts ได้ |
โค้ดแรก
src/index.ts:
function greet(name: string): string {
return `สวัสดี ${name} จาก TypeScript`;
}
const message: string = greet("โลก");
console.log(message);รันตอนพัฒนา — tsx
npx tsx src/index.tsสวัสดี โลก จาก TypeScripttsx strip type ออกแล้วรันด้วย Node — ไม่ทำ type check (เน้นความเร็ว) type check ทำแยกด้วย tsc --noEmit (ดูข้างล่าง)
watch mode:
npx tsx watch src/index.ts # รันใหม่อัตโนมัติเมื่อแก้ไฟล์type check ทั้งโปรเจกต์ — tsc –noEmit
npx tsc --noEmitตรวจ type ทุกไฟล์โดยไม่สร้าง output — ใช้ตอน CI และก่อน commit
const x: number = "hello";
// ~ Type 'string' is not assignable to type 'number'.npx tsc --noEmit
# src/index.ts:1:7 - error TS2322: Type 'string' is not assignable to type 'number'.build เป็น JS — tsc
npx tscสร้าง dist/index.js (+ .js.map) ตาม outDir — เอาไป deploy รันด้วย node dist/index.js
scripts ใน package.json
{
"scripts": {
"dev": "tsx watch src/index.ts",
"build": "tsc",
"start": "node dist/index.js",
"typecheck": "tsc --noEmit"
}
}npm run dev # พัฒนา
npm run typecheck # ตรวจ type
npm run build # สร้าง JSeditor — VS Code
VS Code มี TypeScript ในตัว (ไม่ต้องลง extension) — ขีดเส้นแดง error สด, autocomplete, jump to definition, rename
ตั้งให้ editor ใช้ TypeScript version ของโปรเจกต์ (ไม่ใช่ที่ติดมากับ VS Code): Ctrl+Shift+P → “TypeScript: Select TypeScript Version” → “Use Workspace Version”
ทางเลือกในการรัน
| เครื่องมือ | ใช้เมื่อ |
|---|---|
tsx | dev บน Node — เร็ว, strip type (ไม่ check) |
ts-node | เดิมนิยม — ช้ากว่า tsx, config ยุ่งกว่า |
tsc | build production เป็น .js |
| Vite / esbuild | โปรเจกต์ frontend / bundler — strip type ให้ |
| Bun / Deno | รัน .ts ได้เลย (มี TS support ในตัว) |
Node 22.6+ (--experimental-strip-types) | Node เริ่มรัน .ts ได้เอง (ยัง experimental) |
เรื่องที่คนมักพลาด
- คิดว่า
tsx/viteทำ type check — ไม่! มันแค่ strip type; type check ต้องtsc --noEmitแยก - ไม่เปิด
"strict": true— พลาดประโยชน์หลักของ TS; เปิดตั้งแต่วันแรก - ติดตั้ง
typescriptเป็น dependency (ไม่ใช่ dev) — ไม่จำเป็นบน production module: "CommonJS"แต่เขียนimportในโปรเจกต์ Node ESM — ตั้ง"module": "NodeNext"+"type": "module"ใน package.json- ไม่ลง
@types/node—process,Buffer,__dirnameเป็น error - แก้ error ด้วย
// @ts-ignoreพร่ำเพรื่อ — ซ่อนบั๊ก; แก้ต้นตอ หรือใช้// @ts-expect-error(จะ error ถ้าบรรทัดนั้นไม่มี error แล้ว)
สรุป
npm i -D typescript tsx @types/node,npx tsc --init→tsconfig.json"strict": trueเสมอ;target ES2022,module NodeNext(Node) หรือbundler(Vite)- dev:
npx tsx watch src/index.ts(strip type, เร็ว, ไม่ check) - type check:
npx tsc --noEmit(แยก — ใช้ตอน CI / ก่อน commit) - build:
npx tsc→dist/*.js - VS Code มี TS ในตัว — เลือก “Use Workspace Version”
บทถัดไปว่าด้วยชนิดข้อมูลพื้นฐาน — string/number/boolean, type inference, และความต่างของ any กับ unknown