Hari 27: TypeScript Performance Tips
50 min
Last updated 09 Apr 2026
Tips Performa TypeScript
1. Gunakan Interface daripada Type untuk Object Shapes yang Besar
// Interface lebih cepat dikompilasi untuk objek kompleks
interface LargeObject { /* ... banyak properti */ }
// vs Type (lebih lambat untuk large unions/intersections)
type LargeObjectType = { /* ... */ };
2. Hindari any — Gunakan unknown
// any: tidak aman, tidak ada type checking
function prosesAny(data: any): void { data.apapun(); } // tidak error meskipun salah
// unknown: aman, harus narrowing dulu
function prosesUnknown(data: unknown): void {
if (typeof data === "string") data.toUpperCase(); // ✅
// data.toUpperCase(); // ❌ Error! harus narrowing
}
3. Batasi Penggunaan Recursive Conditional Types
// Bisa membuat TypeScript lambat
type DeepNested = N extends 0
? T
: DeepNested<{ value: T }, []>; // Hindari rekursi dalam
// Lebih baik: batasi depth
type Depth1 = { value: T };
type Depth2 = { value: Depth1 };
💡
Notice: Type predicate (item): item is Item & { aktif: true } memungkinkan TypeScript tahu bahwa setelah filter, aktif pasti true. Ini lebih akurat dari filter(i => i.aktif).
Assignment
Refactor kode berikut: ganti any dengan tipe yang tepat, tambahkan narrowing yang proper. Tampilkan hasil.
Expected output:
Budi, Cici
TS
index.ts
Solution
Output