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/node
  • typescript — คอมไพเลอร์ tsc + type checker
  • tsx — รันไฟล์ .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
สวัสดี โลก จาก TypeScript

tsx 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        # สร้าง JS

editor — 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”

ทางเลือกในการรัน

เครื่องมือใช้เมื่อ
tsxdev บน Node — เร็ว, strip type (ไม่ check)
ts-nodeเดิมนิยม — ช้ากว่า tsx, config ยุ่งกว่า
tscbuild 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