Modules & Declaration — TypeScript

Import/Export types: // types.ts export interface User { nama: string; umur: number; } export type ID = string | number; // app.ts import type { User, ID } from

Import/Export types:

// types.ts
export interface User { nama: string; umur: number; }
export type ID = string | number;

// app.ts
import type { User, ID } from "./types";
// `import type` hanya import tipe (dihapus saat kompilasi)

Declaration files (.d.ts): File yang hanya berisi deklarasi tipe, tanpa implementasi:

// math.d.ts
declare function tambah(a: number, b: number): number;
declare const PI: number;

Ambient declarations — untuk library JS tanpa tipe:

declare module "old-library" {
  export function doSomething(x: string): void;
}

@types packages:

npm install --save-dev @types/lodash
// Otomatis TypeScript mengenali tipe lodash

Kebanyakan library populer sudah menyediakan tipe bawaan atau lewat @types/*.

Yang akan kamu pelajari