File tsconfig.json mengatur bagaimana TypeScript bekerja di project kamu.
Membuat tsconfig:
$ npx tsc --init
Opsi paling penting:
{
"compilerOptions": {
"target": "ES2020", // JavaScript output target
"module": "ESNext", // sistem module
"strict": true, // aktifkan SEMUA strict checks ✅
"outDir": "./dist", // folder output
"rootDir": "./src", // folder source code
"esModuleInterop": true, // import CommonJS modules
"skipLibCheck": true, // percepat compile
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"], // file yang dicompile
"exclude": ["node_modules"] // file yang diabaikan
}
Strict mode ("strict": true) mengaktifkan:
strictNullChecks—nulldanundefinedharus ditanganinoImplicitAny— tidak boleh ada typeanyyang implisitstrictFunctionTypes— function parameter checking lebih ketatstrictPropertyInitialization— property class harus diinisialisasi
Tips per framework:
| Framework | Target | Module |
|---|---|---|
| React (Vite) | ESNext | ESNext |
| Node.js | ES2022 | NodeNext |
| Library | ES2020 | CommonJS + ESNext |
Menjalankan TypeScript:
npx tsc— compile ke JavaScriptnpx tsc --watch— compile otomatis saat file berubahnpx tsx src/index.ts— jalankan langsung tanpa compile (development)
Tips: Selalu gunakan
"strict": true. Lebih baik strict dari awal daripada menambahkan nanti ke project yang sudah besar.