Learn
← Previous Next →

Utility Types

10 min Last updated 23 Mar 2026

Utility Types di TypeScript

TypeScript menyediakan beberapa utility types bawaan yang memudahkan transformasi tipe yang umum dibutuhkan.

Partial<T>

Membuat semua property menjadi opsional — sangat berguna untuk fungsi update:

interface User {
  id: number;
  name: string;
  email: string;
}

function updateUser(id: number, updates: Partial<User>) {
  // updates boleh hanya berisi sebagian property
}

updateUser(1, { name: "Budi" }); // ✓ Valid

Required<T> dan Readonly<T>

// Required — semua property menjadi wajib
type RequiredUser = Required<Partial<User>>;

// Readonly — tidak ada property yang bisa diubah
const config: Readonly<{ apiUrl: string; timeout: number }> = {
  apiUrl: "https://api.example.com",
  timeout: 5000,
};
// config.apiUrl = "..."; // Error!

Pick<T, K> dan Omit<T, K>

interface Article {
  id: number;
  title: string;
  content: string;
  author: string;
  publishedAt: Date;
}

// Pick — ambil hanya beberapa property
type ArticlePreview = Pick<Article, "id" | "title" | "author">;

// Omit — ambil semua kecuali beberapa
type CreateArticle = Omit<Article, "id" | "publishedAt">;

Record<K, V>

type StatusMap = Record<"active" | "inactive" | "pending", string>;

const statusLabels: StatusMap = {
  active: "Aktif",
  inactive: "Tidak Aktif",
  pending: "Menunggu",
};