Learn
← Previous Next →

Hari 9: Record, Extract, Exclude & NonNullable

60 min Last updated 09 Apr 2026

Record — Object dengan Key dan Value Tipe Tertentu

// Record: buat object type dengan key dan value tertentu
type KategoriKursus = "programming" | "design" | "marketing";
type InfoKategori = { nama: string; jumlah: number; warna: string };

const kategoriMap: Record = {
    programming: { nama: "Programming", jumlah: 50, warna: "#7c3aed" },
    design:      { nama: "Design",      jumlah: 20, warna: "#ec4899" },
    marketing:   { nama: "Marketing",   jumlah: 15, warna: "#f59e0b" },
};

// Record sebagai lookup table
type StatusCode = 200 | 201 | 400 | 401 | 404 | 500;
const httpMessages: Record = {
    200: "OK", 201: "Created", 400: "Bad Request",
    401: "Unauthorized", 404: "Not Found", 500: "Internal Server Error",
};

Extract, Exclude & NonNullable

type Semua = "a" | "b" | "c" | "d" | "e";

// Extract: ambil yang ada di keduanya
type Vowel = Extract;  // "a" | "e"

// Exclude: buang yang ada di yang kedua
type Consonant = Exclude; // "b" | "c" | "d"

// NonNullable: buang null dan undefined
type MaybeStr = string | null | undefined;
type Str = NonNullable; // string

💡 Notice: Record<string, number> adalah tipe untuk "dictionary". reduce<Record<...>> menentukan tipe akumulator secara eksplisit.

Assignment

Buat Record<string, number> untuk menghitung frekuensi kata dalam kalimat. Tampilkan kata yang muncul lebih dari 1 kali.

Expected output:

budi: 2x
typescript: 2x
TS index.ts
Solution
Output