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โฆ
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
๐ 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:
- Create a type for the kitchen (they donโt need customer info)
- Create a type for updating an order (maybe change toppings)
- Create a type for the delivery driver (they need address but not payment)
- 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