Skip to main content

What does Partial<T> do?

1. What Partial<T> does

Partial<T> makes every property of type T optional.

That means every field of interface T that used to be required becomes optional (?).


Example

javascript
interface User { id: number; name: string; email: string; } type PartialUser = Partial<User>;

Now PartialUser is equivalent to:

javascript
{ 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:

javascript
type Partial<T> = { [P in keyof T]?: T[P]; };

This is the built-in implementation of Partial inside TypeScript.


Usage example

javascript
interface Config { port: number; host: string; } const cfg: Partial<Config> = { port: 8080, // you can specify a single field };

Everything works even if only port is 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

javascript
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 allowed

Even the required field theme is 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

javascript
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 name and age, because Partial only made the info field 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:

javascript
type DeepPartial<T> = { [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P]; };

Example with DeepPartial

javascript
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

ScenarioExample
Updating data (a patch)updateUser(id: number, data: Partial<User>)
Configurations and settingsPartial<Config> lets you specify only the fields you need
Building mock objects in testsyou do not need to specify every field of the interface
In forms and DTOswhen values arrive partially

Summary

QuestionAnswer
What does Partial<T> doMakes every property of type T optional
How does it change the interfaceAdds ? to every property ([P in keyof T]?: T[P])
What happens to required propertiesThey become optional
Does it work with nested objectsNo, only at the surface level
How to make nested ones optional tooUse a custom DeepPartial<T>

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.