

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Single Source of Truth
incomplete
2: Partial Utility Type
incomplete
3: Required Utility Type
incomplete
4: Readonly Utility Type
incomplete
5: Record Utility Type
incomplete
6: Pick Utility Type
incomplete
7: Omit Utility Type
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
There are several built-in utility types that transform existing types into new ones. One of the most useful is Partial<T>, which makes all properties of a type optional. For example:
type User = {
id: string;
name: string;
email: string;
};
// Without Partial
function updateUser(
userId: string,
userInfo: {
id?: string;
name?: string;
email?: string;
},
) {
// ...
}
// With Partial
function updateUser(userId: string, userInfo: Partial<User>) {
// ...
}
Instead of copy/pasting the type definition, the Partial<T> utility type allows us to generate a new type based on an existing one. That also means if the original is ever updated, the new type created with Partial<T> type will automatically have those changes!
Partial<T> only makes the top-level properties optional. For example:
type User = {
id: string;
name: string;
preferences: {
theme: string;
notifications: boolean;
};
};
If we use Partial<User>, the resulting type would look like this:
// same as 'type LooseyGooseyUser = Partial<User>'
type LooseyGooseyUser = {
id?: string;
name?: string;
preferences?: {
theme: string;
notifications: boolean;
};
};
The theme and notifications properties are still required (assuming preferences is provided).
Fix the compile-time bug in the updateUser function. It should accept a User object, but where each property is optional.