TypeScript tsconfig.json เจาะลึก
strict — ชุดของ flag
"strict": true เปิดทั้งหมดนี้:
| flag | ทำอะไร |
|---|---|
noImplicitAny | พารามิเตอร์/ตัวแปรที่ไม่มี type = error (ไม่ปล่อยเป็น any เงียบ ๆ) |
strictNullChecks | null/undefined เป็น type แยก ต้องจัดการ (บทที่ 24) |
strictFunctionTypes | ตรวจ function parameter แบบ contravariant (เข้มขึ้น) |
strictBindCallApply | .bind/.call/.apply ตรวจ argument |
strictPropertyInitialization | class field ต้องถูก init |
noImplicitThis | this ที่เป็น any = error |
alwaysStrict | ใส่ "use strict" ใน output |
useUnknownInCatchVariables | catch (e) เป็น unknown (บทที่ 26) |
เปิด strict เสมอ สำหรับโปรเจกต์ใหม่ โปรเจกต์เก่าที่ migrate — เปิดทีละ flag
flag เพิ่มความเข้ม (ไม่อยู่ใน strict)
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true, // arr[i] / obj[key] เป็น T | undefined (บทที่ 24)
"noImplicitReturns": true, // ทุก path ต้อง return (ถ้า function คืนค่า)
"noFallthroughCasesInSwitch": true, // case ที่ไม่มี break = error
"noImplicitOverride": true, // override ต้องมี keyword `override` (บทที่ 18)
"exactOptionalPropertyTypes": true, // { x?: string } ≠ { x?: string | undefined }
"noPropertyAccessFromIndexSignature": true, // obj.foo กับ index signature → ต้อง obj["foo"]
"verbatimModuleSyntax": true, // type import ต้องเขียน `type` ชัด (บทที่ 22)
"allowUnreachableCode": false,
"allowUnusedLabels": false
}
}noUncheckedIndexedAccess กับ exactOptionalPropertyTypes เข้มมาก — บางทีมเปิด บางทีมไม่ (verbose ขึ้น)
target — เวอร์ชัน JS ที่ compile ออกมา
{ "target": "ES2022" }"ES2022"— Node 18+, เบราว์เซอร์สมัยใหม่ (มี top-level await,Array.at, class fields,Error.cause,#private)"ES2020"— รองรับกว้างขึ้น"ESNext"— ทุกอย่างล่าสุด (bundler จะจัดการ downlevel เอง)
target ต่ำ → TS แปลง syntax ใหม่เป็นเก่า (async → generator, ?? → ternary) — bundle ใหญ่ขึ้น
module — ระบบ module ของ output
{ "module": "NodeNext" } // หรือ "ESNext", "Preserve"| ค่า | ใช้เมื่อ |
|---|---|
"NodeNext" | Node ที่รันด้วย node ตรง ๆ — auto ตาม package.json "type" (ต้องนามสกุล .js ใน import) |
"ESNext" / "Preserve" | โปรเจกต์ที่ bundler จัดการ module |
"CommonJS" | legacy Node CJS |
"moduleResolution" มักตามคู่: NodeNext → NodeNext, ESNext → "bundler"
lib — API ที่มีให้
{ "lib": ["ES2022", "DOM", "DOM.Iterable"] }"ES2022"—Array.prototype.at,Object.hasOwn,Error.cause, …"DOM"—document,window,fetch,HTMLElement(frontend เท่านั้น)"WebWorker"— worker context- ถ้าไม่ระบุ
lib— default ตามtarget
Node ล้วน: อย่าใส่ "DOM" — ไม่งั้น document, localStorage ที่ไม่มีจริงจะไม่ error
emit options
{
"outDir": "dist",
"rootDir": "src",
"declaration": true, // สร้าง .d.ts ด้วย (สำหรับ library)
"declarationMap": true, // .d.ts.map (jump to source)
"sourceMap": true, // .js.map (debug)
"noEmit": true, // ไม่สร้าง output (เมื่อใช้ bundler compile จริง — tsc แค่ check)
"isolatedModules": true, // บังคับให้แต่ละไฟล์ compile แยกได้ (bundler ต้องการ)
"removeComments": true
}รูปแบบที่พบ:
- library:
declaration: true,outDir: "dist", ใช้tscbuild จริง - app + bundler:
noEmit: true,isolatedModules: true—tscแค่ type check, bundler สร้าง JS
path aliases
{
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@components/*": ["src/components/*"]
}
}(บทที่ 22 — runtime ต้องมี tool รู้จักด้วย)
include / exclude / files
{
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}include default ["**/*"]; exclude default ["node_modules", ...]
project references — monorepo
// packages/shared/tsconfig.json
{ "compilerOptions": { "composite": true, "declaration": true } }
// packages/app/tsconfig.json
{
"references": [{ "path": "../shared" }],
"compilerOptions": { }
}tsc --build # build ตาม dependency graph, cache ผล (incremental)extends — แชร์ config
// tsconfig.base.json — config ร่วม
{ "compilerOptions": { "strict": true, "target": "ES2022" } }
// tsconfig.json
{ "extends": "./tsconfig.base.json", "include": ["src"] }หรือ extends จาก package: "extends": "@tsconfig/node20/tsconfig.json" (@tsconfig/* มี preset สำหรับ Node/Vite/etc.)
tsconfig ที่แนะนำสำหรับ Node app
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2022"],
"strict": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"verbatimModuleSyntax": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "src",
"sourceMap": true,
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}เรื่องที่คนมักพลาด
- ไม่เปิด
strict— พลาดประโยชน์หลักของ TS "lib": ["DOM"]ในโปรเจกต์ Node —document,localStorageที่ไม่มีจริงไม่ errortargetต่ำเกินโดยไม่จำเป็น — bundle ใหญ่จาก downlevel syntaxmodule: "CommonJS"แต่เขียน ESM — ตั้งNodeNext+"type": "module"ใน package.jsonpathsแล้ว runtime พัง —tscไม่ rewrite (บทที่ 22)skipLibCheck: falseในโปรเจกต์ใหญ่ — ช้ามาก;trueแนะนำ- ไม่มี
noEmitเมื่อใช้ bundler —tscสร้าง.jsปนกับ output ของ bundler
สรุป
"strict": true= ชุด flag (noImplicitAny,strictNullChecks, …) — เปิดเสมอ- flag เข้มเพิ่ม:
noUncheckedIndexedAccess,noImplicitOverride,verbatimModuleSyntax,exactOptionalPropertyTypes target— เวอร์ชัน JS output (ES2022ทันสมัย);lib— API (DOMfrontend เท่านั้น)module/moduleResolution:NodeNext(Node) หรือESNext/"bundler"(Vite)- app + bundler:
noEmit: true+isolatedModules(tsc แค่ check); library:declaration: true+tscbuild extends(@tsconfig/node20),references+tsc --build(monorepo)
บทถัดไปว่าด้วย declaration files — .d.ts, @types/* จาก DefinitelyTyped, declare และการเขียน .d.ts เอง