What does Partial<T> do?
1. What Partial<T> does
Partial<T>makes every property of typeToptional.
That means every field of interface T that used to be required
becomes optional (?).
Example
interface User {
id: number;
name: string;
email: string;
}
type PartialUser = Partial<User>;Now PartialUser is equivalent to:
{
id?: number;
name?: string;
email?: string;
}All properties became optional (
?).
2. How Partial<T> changes an interface's properties
It walks through every key of interface T
and makes the property optional via a mapped type:
type Partial<T> = {
[P in keyof T]?: T[P];
};This is the built-in implementation of
Partialinside TypeScript.
Usage example
interface Config {
port: number;
host: string;
}
const cfg: Partial<Config> = {
port: 8080, // you can specify a single field
};Everything works even if only
portis specified.
3. What Partial<T> does to required properties
Partial<T> cancels the required status of every field,
even if it was required in the original type.
Example
interface Settings {
theme: string;
notifications: boolean;
}
type PartialSettings = Partial<Settings>;
const s1: PartialSettings = {}; // an empty object is allowed
const s2: PartialSettings = { theme: "dark" }; // partial is allowedEven the required field
themeis now optional.
4. Can Partial be applied to nested objects?
Great question, and this is the key point where many people get confused.
By default
Partial<T>only works at the first level.
That is, nested objects do not become optional inside themselves.
Example
interface Profile {
id: number;
info: {
name: string;
age: number;
};
}
type PartialProfile = Partial<Profile>;
const p: PartialProfile = {
info: {}, // Error: name and age are expected
};TypeScript still requires
nameandage, becausePartialonly made theinfofield itself optional, not its inner properties.
5. How to make a deep Partial (recursive)
If you need nested properties to also become optional, you can create your own DeepPartial type:
type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};Example with DeepPartial
interface Profile {
id: number;
info: {
name: string;
age: number;
};
}
const user: DeepPartial<Profile> = {
info: {
name: "Tim", // only a single field can be specified
},
};Now nested fields are optional too.
6. When Partial<T> is used
| Scenario | Example |
|---|---|
| Updating data (a patch) | updateUser(id: number, data: Partial<User>) |
| Configurations and settings | Partial<Config> lets you specify only the fields you need |
| Building mock objects in tests | you do not need to specify every field of the interface |
| In forms and DTOs | when values arrive partially |
Summary
| Question | Answer |
|---|---|
What does Partial<T> do | Makes every property of type T optional |
| How does it change the interface | Adds ? to every property ([P in keyof T]?: T[P]) |
| What happens to required properties | They become optional |
| Does it work with nested objects | No, only at the surface level |
| How to make nested ones optional too | Use a custom DeepPartial<T> |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.