Apa Itu TypeScript?
TypeScript adalah superset dari JavaScript yang menambahkan static typing. Dikembangkan oleh Microsoft pada tahun 2012, TypeScript dikompilasi menjadi JavaScript biasa. TypeScript membantu menangkap error pada compile time, bukan runtime, sehingga kode lebih aman dan maintainable.
Mengapa TypeScript?
- Type Safety — Menangkap error sebelum runtime
- Better IDE Support — Autocomplete, refactoring, navigation
- Scalability — Cocok untuk project besar
- Modern Features — Decorators, generics, enums, interfaces
- Industry Standard — Digunakan oleh Google, Microsoft, Airbnb, Slack
- Easy Migration — Bisa ditambahkan ke project JavaScript secara bertahap
Roadmap Belajar TypeScript
Level 1: Beginner (Dasar TypeScript)
- Setup — npm install typescript, tsc, tsconfig.json
- Tipe Dasar — string, number, boolean, array, tuple, enum
- Type Inference — TypeScript menebak tipe otomatis
- Union Types — string | number
- Literal Types — “success” | “error” | “loading”
- Function Types — Parameter dan return type annotation
- Object Types — Interface, type alias
- Optional Properties — name?: string
- Readonly — readonly property
Level 2: Intermediate
- Generics — Generic functions, classes, interfaces, constraints
- Utility Types — Partial, Required, Pick, Omit, Record, Exclude, Extract
- Mapped Types — Transform properties
- Conditional Types — T extends U ? X : Y
- Type Guards — typeof, instanceof, in, custom type guards
- Narrowing — Type narrowing dengan control flow
- Namespaces & Modules — import/export, namespace
Level 3: Advanced
- Decorators — Class, method, property decorators
- Mixins — Composing classes
- Advanced Types — Template literal types, recursive types
- Declaration Files — .d.ts files, DefinitelyTyped
- Compiler API — Custom transformers
Level 4: Expert
- React + TypeScript — Typing props, state, hooks, events
- Node.js + TypeScript — Express, NestJS
- Testing — Jest, Vitest dengan TypeScript
- Build Tools — Vite, Webpack, esbuild, SWC
- Monorepo — Nx, Turborepo, pnpm workspaces
Contoh Kode TypeScript
// Tipe dasar
let nama: string = "Muhamad Widiono";
let umur: number = 30;
let aktif: boolean = true;
let hobi: string[] = ["Coding", "Blogging", "Gaming"];
// Interface
interface Produk {
id: number;
nama: string;
harga: number;
diskon?: number; // optional
readonly sku: string;
}
// Type alias
type Status = "active" | "inactive" | "banned";
// Generic function
function identity(arg: T): T {
return arg;
}
// Generic dengan constraint
function getLength(item: T): number {
return item.length;
}
// Class
class User {
constructor(
public nama: string,
private password: string,
readonly id: number
) {}
greet(): string {
return `Halo, ${this.nama}!`;
}
}
// Utility types
interface UserFull {
id: number;
nama: string;
email: string;
password: string;
}
type UserPublic = Omit;
type UserPartial = Partial;
type UserPick = Pick;
// Enum
enum Hari {
Senin = 1,
Selasa,
Rabu,
Kamis,
Jumat,
Sabtu,
Minggu
}
// Async/Await dengan typing
async function fetchUser(id: number): Promise {
const response = await fetch(`/api/users/${id}`);
return response.json();
}
// Type guard
function isString(value: unknown): value is string {
return typeof value === "string";
}
// Mapped type
type Readonly = {
readonly [P in keyof T]: T[P];
};
// Template literal type
type EventName = `on${Capitalize}`;
Tips Belajar TypeScript
- Pelajari JavaScript dulu — TypeScript adalah superset JS
- Mulai dengan strict mode — “strict”: true di tsconfig
- Jangan over-type — Biarkan TypeScript infer tipe jika bisa
- Gunakan utility types — Partial, Pick, Omit sangat berguna
- Pelajari generics — Konsep penting untuk reusable code
- Baca TypeScript Handbook — typescriptlang.org/docs
Tools yang Dibutuhkan
| Tool | Deskripsi |
|---|---|
| Node.js & NPM | Runtime dan package manager |
| TypeScript Compiler (tsc) | Compile TS ke JS |
| VS Code | IDE terbaik untuk TypeScript |
| ESLint + Prettier | Linter dan formatter |
| Vite / Webpack | Build tool |
Kesimpulan
TypeScript adalah investasi yang sangat berharga untuk developer JavaScript. Dengan type safety, IDE support yang lebih baik, dan scalability, TypeScript menjadi standar industri untuk project besar. Mulai dengan mengkonversi project JavaScript ke TypeScript secara bertahap!
“TypeScript is JavaScript that scales.” — Anders Hejlsberg




