

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
It's incredibly common for a TypeScript codebase to amass a truly absurd number of custom type definitions - hundreds of interfaces and types, all with slightly different numbers of fields for a lot of the same "entities". You might run into crazy stuff like:
interface User {
id: string;
name: string;
email: string;
age: number;
}
interface UserWithoutId {
name: string;
email: string;
age: number;
}
This is bad. We generally try to avoid redefining the same types over and over, and instead try to follow a "single source of truth" approach. For example, here we could refactor a bit:
interface UserWithoutId {
name: string;
email: string;
age: number;
}
interface User extends UserWithoutId {
id: string;
}
There are other techniques to do this even more cleanly that we'll cover later in this chapter.
In other words, we try to define our types once, and build type systems that rely on inference and type transformations to derive the types we need automatically. That way, when we make changes, we only have to do it in one place. In our second example, updating UserWithoutId will now automatically update User as well: a big win.