What if you could mix and match types like LEGO blocks to build exactly what you need without mistakes?
Why Combining utility types in Typescript? - Purpose & Use Cases
Imagine you have a big box of LEGO pieces, and you want to build a specific model by picking only certain pieces and changing some colors manually every time.
Manually picking and changing pieces is slow and easy to mess up. You might forget a piece or pick the wrong color, and fixing it takes even more time.
Combining utility types in TypeScript lets you quickly create new types by mixing and matching existing ones. It's like having a magic LEGO sorter that picks and colors pieces perfectly for you.
type User = { name: string; age: number; email: string; };
type PartialUser = { name?: string; age?: number; email?: string; };type User = { name: string; age: number; email: string; };
type PartialUser = Partial<User> & Pick<User, 'email'>;You can build complex, precise types easily, making your code safer and faster to write.
When building a form, you might want to accept only some user info as optional but require the email always. Combining utility types lets you express this clearly and simply.
Manual type changes are slow and error-prone.
Combining utility types automates and simplifies type creation.
This leads to safer, clearer, and more maintainable code.
Practice
Solution
Step 1: Understand Partial and Readonly
Partial<T>makes all properties optional, andReadonly<T>makes all properties readonly.Step 2: Combine utility types correctly
WrappingPartial<T>insideReadonly<>makes all optional properties readonly as well.Final Answer:
Readonly<Partial<T>> -> Option AQuick Check:
Readonly + Partial = Readonly<Partial<T>> [OK]
- Reversing the order of Partial and Readonly
- Using Pick or Omit instead of Partial/Readonly
- Assuming Pick makes properties optional
Solution
Step 1: Understand Omit and Readonly
Omit<T, 'a' | 'b'>removes properties 'a' and 'b' from T.Readonly<>makes all properties readonly.Step 2: Apply Readonly after Omit
ApplyingReadonlyto the result ofOmitmakes the remaining properties readonly.Final Answer:
Readonly<Omit<T, 'a' | 'b'>> -> Option CQuick Check:
Omit then Readonly = Readonly<Omit<T, 'a' | 'b'>> [OK]
- Applying Omit after Readonly (wrong order)
- Using Pick instead of Omit
- Forgetting to use quotes around property names
type T = { a: number; b: string; c: boolean; }, what is the resulting type of Partial<Pick<T, 'a' | 'c'>>?Solution
Step 1: Apply Pick to select properties
Pick<T, 'a' | 'c'>selects only properties 'a' and 'c' from T.Step 2: Apply Partial to make selected properties optional
Partial<>makes 'a' and 'c' optional, so the type becomes{ a?: number; c?: boolean; }.Final Answer:
{ a?: number; c?: boolean; } -> Option AQuick Check:
Partial + Pick = optional picked props [OK]
- Including property 'b' which was not picked
- Forgetting Partial makes properties optional
- Assuming Partial affects all original properties
type U = Partial<Readonly<Omit<T, 'x'>>>Solution
Step 1: Analyze the order of utility types
The type appliesOmitfirst, removing property 'x', thenReadonlyto make remaining props readonly, thenPartialto make them optional.Step 2: Check if this order is valid
This order is valid and results in optional readonly properties except 'x' which is omitted.Final Answer:
No error; the type combination is valid -> Option BQuick Check:
Omit -> Readonly -> Partial is valid [OK]
- Thinking Partial cannot follow Readonly
- Assuming Omit must be last
- Confusing order of utility types
T that has only properties 'id' and 'name' readonly, and all other properties optional. Which combination achieves this?Solution
Step 1: Make 'id' and 'name' readonly
UseReadonly<Pick<T, 'id' | 'name'>>to select and make these properties readonly.Step 2: Make all other properties optional
UsePartial<Omit<T, 'id' | 'name'>>to make the rest optional.Step 3: Combine with intersection (&)
Use intersection&to merge both types into one.Final Answer:
Readonly<Pick<T, 'id' | 'name'>> & Partial<Omit<T, 'id' | 'name'>> -> Option DQuick Check:
Readonly Pick & Partial Omit = desired type [OK]
- Using logical AND (&&) instead of intersection (&)
- Applying Partial to readonly properties
- Omitting intersection operator
Ready to prove it?
Pass the test and this pattern moves unknown
