Suggest an editImprove this articleRefine the answer for “What does Partial<T> do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Partial<T>** makes every property of type `T` optional, walking through each key of the type and adding `?` to it via the mapped type `[P in keyof T]?: T[P]`. **Key point:** by default Partial only works at the first level - nested objects do not become optional internally; that requires a custom recursive `DeepPartial` type.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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>` |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.