Understanding How Built-in Utilities Work
What you'll learn: You'll discover that Required<T>, Readonly<T>, and Partial<T> are not magic—they're mapped types using the exact modifier syntax you've already learned.
The Wizard Behind the Curtain
Remember learning to add and remove readonly and optional modifiers with + and -? The TypeScript standard library uses exactly that syntax to build its utility types. There's no special compiler magic—just mapped types with modifiers.
Let's peek behind the scenes:
// How Partial<T> is actually implemented
type Partial<T> = {
[P in keyof T]?: T[P];
};
// How Required<T> is actually implemented
type Required<T> = {
[P in keyof T]-?: T[P];
};
// How Readonly<T> is actually implemented
type Readonly<T> = {
readonly [P in keyof T]: T[P];
};
Why This Matters
Understanding that these utilities are just mapped types empowers you:
- You can read them: No mystery about what
Partial<User>does—it iterates over keys and adds? - You can build your own: The same patterns work for custom utilities
- You understand limitations: Since they're shallow mappings, they don't transform nested objects
When you write Partial<Config>, TypeScript literally iterates through every property in Config and adds the optional modifier. That's it. Same mechanism as your custom mapped types, just packaged with a convenient name.
Key Takeaway: Built-in utilities like Required<T>, Readonly<T>, and Partial<T> are simply mapped types using modifier syntax (-?, readonly, ?)—you already know how to build them yourself.