โ† Back to list

TypeScript Utility Types Explained Like Youโ€™re 10! ๐ŸŽ

Hey there! Letโ€™s imagine youโ€™re organizing your school backpack. TypeScript utility types are like special backpack organizers that helpโ€ฆ

yetesfa alemayehu ยท 2026-03-09 16:13 ยท 0 claps ยท 5.0 min read
#typescript-utility-types #omit #partial #typescript #nextjs
Open on Medium โ†—
Wiki topics: ๐ŸŒ ยท Web Development

TypeScript Utility Types Explained Like Youโ€™re 10! ๐ŸŽ

Hey there! Letโ€™s imagine youโ€™re organizing your school backpack. TypeScript utility types are like special backpack organizers that help you keep everything neat and safe!

Photo by Josiah Weiss on Unsplash

Photo by Josiah Weiss on Unsplash

๐ŸŽ’ Your Backpack = Your Data

Think of your data like a backpack with different compartments:

interface Backpack {
  id: string;          // Your name tag
  books: string[];     // Your school books
  lunch: string;       // Your sandwich
  diary: string;       // Your secret diary
  money: number;       // Your lunch money
  oldHomework: Date;   // Last week's homework
}

Now letโ€™s learn how to organize this backpack! ๐ŸŽฏ

๐ŸŽฏ 1. PICK โ€” โ€œOnly Take What You Needโ€

The Problem:

When you go to class, you donโ€™t need your ENTIRE backpack. You just need specific things!

The Solution:

// ๐ŸŽ’ Full backpack
interface Backpack {
  id: string;
  books: string[];
  lunch: string;
  diary: string;     // โŒ Too private for class!
  money: number;     // โŒ Shouldn't bring to class!
  oldHomework: Date;
}
// ๐Ÿ“š What you actually need for class:
type ClassItems = Pick<Backpack, 'id' | 'books' | 'lunch'>;
// Result: { id: string; books: string[]; lunch: string; }
// Now you have:
// โœ… id (so teacher knows it's yours)
// โœ… books (for learning)
// โœ… lunch (for break time)
// โŒ NO diary (too private!)
// โŒ NO money (should be safe at home!)

Real Life Example:

// Going to math class
function goToMathClass(items: Pick<Backpack, 'books' | 'lunch'>) {
  console.log("Taking these to math class:");
  console.log("Books:", items.books);
  console.log("Lunch:", items.lunch);
  // โŒ items.diary - CAN'T access private diary!
}
goToMathClass({
  books: ["Math Book", "Calculator"],
  lunch: "Sandwich"
});

๐Ÿšซ 2. OMIT โ€” โ€œLeave Behind What You Donโ€™t Needโ€

The Problem:

Sometimes itโ€™s easier to say what you DONโ€™T want rather than list everything you DO want.

The Solution:

// ๐ŸŽ’ Full backpack
interface Backpack {
  id: string;
  books: string[];
  lunch: string;
  diary: string;     // โŒ Don't want this!
  money: number;     // โŒ Don't want this!
  oldHomework: Date; // โŒ Don't want this!
}
// ๐Ÿซ Safe items for school:
type SchoolSafe = Omit<Backpack, 'diary' | 'money' | 'oldHomework'>;
// Result: { id: string; books: string[]; lunch: string; }
// This is like saying:
// "Take everything EXCEPT diary, money, and old homework"

Real Life Example:

// Mom checking your backpack for school
function checkBackpackForSchool(backpack: Omit<Backpack, 'diary' | 'money'>) {
  console.log("โœ… Backpack is school-safe!");
  console.log("Has:", backpack.books, backpack.lunch);
  // โŒ backpack.diary - Mom can't see your diary!
  // โŒ backpack.money - No money needed for school!
}
checkBackpackForSchool({
  id: "John's Backpack",
  books: ["Science", "English"],
  lunch: "Apple",
  oldHomework: new Date()
});

โ“ 3. PARTIAL โ€” โ€œMaybe Bring These Thingsโ€

The Problem:

When youโ€™re not sure what youโ€™ll need, you want the OPTION to bring things.

The Solution:

// ๐ŸŽ’ Normally you need everything:
interface Homework {
  math: string;
  science: string;
  english: string;
  history: string;
}
// ๐Ÿ“ But some days you only do SOME homework:
type SomeHomework = Partial<Homework>;
// Result: { math?: string; science?: string; english?: string; history?: string; }
// Now you can have:
// โœ… Just math homework
// โœ… Math and science homework  
// โœ… All homework
// โœ… No homework (empty object)

Real Life Example:

// Teacher collecting homework
function collectHomework(homework: Partial<Homework>) {
  if (homework.math) {
    console.log("๐Ÿ“ Math homework:", homework.math);
  }
  if (homework.science) {
    console.log("๐Ÿ”ฌ Science homework:", homework.science);
  }
  // All subjects are OPTIONAL - you might not have done them all
}
// All of these work:
collectHomework({ math: "Page 25" });
collectHomework({ science: "Lab report", english: "Essay" });
collectHomework({}); // No homework today! ๐Ÿ˜…

โœ… 4. REQUIRED โ€” โ€œYou MUST Bring These!โ€

The Problem:

Sometimes things are optional, but for important trips, you NEED everything.

The Solution:

// ๐ŸŽ’ Normally some items are optional:
interface FieldTripItems {
  permissionSlip?: string;    // ๐Ÿ˜ฌ Maybe your parents forgot to sign
  lunch?: string;             // ๐Ÿ˜ฌ Maybe you'll buy lunch
  money?: number;             // ๐Ÿ˜ฌ Maybe you won't need money
  jacket?: string;            // ๐Ÿ˜ฌ Maybe it won't be cold
}
// ๐ŸšŒ For the field trip, everything is REQUIRED:
type FieldTripMustHaves = Required<FieldTripItems>;
// Result: { permissionSlip: string; lunch: string; money: number; jacket: string; }
// Now you MUST have:
// โœ… Permission slip (signed!)
// โœ… Lunch (can't buy there!)
// โœ… Money (for souvenirs!)
// โœ… Jacket (it will be cold!)

Real Life Example:

// Teacher checking field trip preparation
function checkFieldTripReadiness(items: Required<FieldTripItems>) {
  console.log("โœ… Permission slip:", items.permissionSlip);
  console.log("โœ… Lunch:", items.lunch);
  console.log("โœ… Money: $", items.money);
  console.log("โœ… Jacket:", items.jacket);
  // ALL are required - no maybe's!
}
// This works:
checkFieldTripReadiness({
  permissionSlip: "Signed by mom",
  lunch: "Sandwich and juice",
  money: 20,
  jacket: "Winter coat"
});
// This would ERROR:
// checkFieldTripReadiness({
//   permissionSlip: "Signed", 
//   lunch: "Sandwich"
//   // โŒ Missing money and jacket!
// });

๐ŸŽฎ Letโ€™s Play With Real Examples!

Game Character Example:

// ๐ŸŽฎ Your video game character:
interface GameCharacter {
  health: number;
  armor: number;
  weapons: string[];
  secretCode: string;    // ๐Ÿคซ Only developers should know!
  password: string;      // ๐Ÿ”’ Super secret!
  level: number;
}
// ๐Ÿ‘ฅ What other players can see:
type PublicCharacter = Pick<GameCharacter, 'health' | 'armor' | 'weapons' | 'level'>;
// OR
type PublicCharacter = Omit<GameCharacter, 'secretCode' | 'password'>;
// Both give you: { health: number; armor: number; weapons: string[]; level: number; }
// โŒ No secret codes or passwords!

Social Media Example:

// ๐Ÿ“ฑ Your social media profile:
interface UserProfile {
  username: string;
  displayName: string;
  bio: string;
  email: string;         // ๐Ÿ”’ Private!
  password: string;      // ๐Ÿ”’ Super private!
  birthDate: Date;       // ๐Ÿ”’ Private!
  posts: string[];
  followers: number;
}
// ๐ŸŒ What the public sees:
type PublicProfile = Omit<UserProfile, 'email' | 'password' | 'birthDate'>;
// ๐Ÿ‘ค What appears in search results:
type SearchResult = Pick<UserProfile, 'username' | 'displayName' | 'bio'>;

๐Ÿงฉ Putting It All Together

School Week Planner:

interface SchoolWeek {
  monday: string[];
  tuesday: string[];
  wednesday: string[];
  thursday: string[];
  friday: string[];
}
// ๐ŸŽฏ This week's focus (only some days):
type ThisWeekFocus = Pick<SchoolWeek, 'monday' | 'wednesday' | 'friday'>;
// ๐Ÿ“… Update your schedule (some days might change):
type WeekUpdates = Partial<SchoolWeek>;
// ๐Ÿšจ Important test week (ALL days required):
type TestWeek = Required<SchoolWeek>;

๐ŸŽฏ Quick Cheat Sheet for Kids!

// ๐ŸŽ’ PICK - "I want THESE specific things"
type WhatIWant = Pick<Backpack, 'books' | 'lunch'>;
// ๐Ÿšซ OMIT - "I want everything EXCEPT these"
type WhatIDontWant = Omit<Backpack, 'diary' | 'money'>;
// โ“ PARTIAL - "Maybe I'll bring these, maybe not"
type MaybeBring = Partial<Backpack>;
// โœ… REQUIRED - "I MUST bring all of these!"
type MustBring = Required<Backpack>;

๐ŸŽ Your Turn to Practice!

Imagine you have a Pizza Order:

interface PizzaOrder {
  size: string;
  crust: string;
  toppings: string[];
  specialInstructions?: string;
  customerName: string;
  phoneNumber: string;      // ๐Ÿ”’ Private!
  address: string;          // ๐Ÿ”’ Private!
  paymentInfo: string;      // ๐Ÿ”’ Super private!
}

Your Challenges:

  1. Create a type for the kitchen (they donโ€™t need customer info)
  2. Create a type for updating an order (maybe change toppings)
  3. Create a type for the delivery driver (they need address but not payment)
  4. Create a type that requires all optional fields

Try it yourself first, then check below! ๐Ÿ‘‡

๐Ÿ† Answer Key

// 1. Kitchen view (no private info)
type KitchenOrder = Omit<PizzaOrder, 'phoneNumber' | 'address' | 'paymentInfo'>;
// 2. Update order (everything optional except maybe order ID in real life)
type OrderUpdate = Partial<PizzaOrder>;
// 3. Delivery driver (needs address but not payment info)
type DeliveryInfo = Pick<PizzaOrder, 'customerName' | 'address'>;
// 4. Complete order (no optional fields)
type CompleteOrder = Required<PizzaOrder>;ty

๐ŸŽ‰ You Did It!

Now you understand TypeScript utility types! Remember:

  • Pick = โ€œI want these!โ€ ๐Ÿ‘‰
  • Omit = โ€œI donโ€™t want these!โ€ ๐Ÿ‘ˆ
  • Partial = โ€œMaybe these?โ€ ๐Ÿค”
  • Required = โ€œMust have these!โ€ โœ…

These are like superpowers for organizing your code โ€” and they work in ANY TypeScript project! ๐Ÿš€

What other examples can you think of? Share your ideas below! ๐Ÿ’ฌ


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
ba2b81d3f86d
slug
typescript-utility-types-explained-like-youre-10-ba2b81d3f86d
url
https://medium.com/@yetesfadev/typescript-utility-types-explained-like-youre-10-ba2b81d3f86d
canonical_url
https://medium.com/@yetesfadev/typescript-utility-types-explained-like-youre-10-ba2b81d3f86d
author_url
https://medium.com/@yetesfadev
status
ok
fetched_at
2026-06-24 11:06:28