Typescript Migration JS To TS Step By Step
Ringkasan
TS kompatibel dengan JS. Migrasi bisa bertahap: jalankan TS di samping JS, lalu perketat tipe pelan-pelan.
Konsep Dasar
- Install:
npm i -D typescript. - Buat
tsconfig.jsondengan"allowJs": trueagar file.jstetap jalan. - Ganti ekstensi
.js→.tssatu per satu. - Tambah
// @ts-checkataucheckJsuntuk cek file JS tanpa convert. - Naikkan strictness bertahap (lihat post terkait).
Cara Kerja / Struktur
// tsconfig.json (awal ramah migrasi)
{
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"noEmit": true,
"strict": false
}
}// utils.ts (mulai dari tipe longgar)
function add(a: number, b: number): number { return a + b; }Catatan Implementasi
- Jangan nyalakan
strictdi hari pertama — error meledak & tim demotivasi. anysebagai jembatan sementara, ganti bertahap.- Build tool (tsc/esbuild/vite) menangani transpile; emit JS.
Kesalahan Umum
- Langsung
strict: trueke seluruh codebase → ribuan error. - Menghapus
.jssebelum yakin TS build jalan.
Glosarium
- allowJs: izinkan file JS ikut dikompilasi.
- checkJs: cek tipe file JS (lewati @ts-check).