Learn
← Previous Next →

Hari 22: TypeScript Configuration & tsconfig

50 min Last updated 09 Apr 2026

tsconfig.json — Konfigurasi TypeScript

{
  "compilerOptions": {
    // Target output
    "target": "ES2022",          // versi JS output
    "module": "ESNext",          // module system
    "lib": ["ES2022", "DOM"],    // library tersedia

    // Type checking
    "strict": true,              // aktifkan semua strict checks
    "noImplicitAny": true,       // error jika tipe tidak jelas
    "strictNullChecks": true,    // null/undefined harus explicit
    "noUncheckedIndexedAccess": true, // array access bisa undefined

    // Output
    "outDir": "./dist",
    "rootDir": "./src",
    "declaration": true,         // generate .d.ts files
    "sourceMap": true,           // untuk debugging

    // Module resolution
    "moduleResolution": "bundler", // untuk Vite/esbuild
    "paths": {
      "@/*": ["./src/*"]         // alias import
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

Strict Mode Checks

// strictNullChecks
function sapa(nama: string | null): string {
    // tanpa strictNullChecks: nama.toUpperCase() lolos compile
    // dengan strictNullChecks: harus handle null
    return nama?.toUpperCase() ?? "Tamu";
}

// noUncheckedIndexedAccess
const arr = [1, 2, 3];
const item = arr[5]; // type: number | undefined (bukan number!)

💡 Notice: Optional chaining (?.) dan nullish coalescing (??) bekerja bersama dengan sempurna. Keduanya dibutuhkan saat menangani data dari API yang mungkin null.

Assignment

Demonstrasikan pentingnya strictNullChecks: buat fungsi yang aman menangani nullable input menggunakan optional chaining dan nullish coalescing.

Expected output:

BUDI
Anonim
Anonim
3
0
Rp 150,000
Rp 0
TS index.ts
Solution
Output