Suggest an editImprove this articleRefine the answer for “What does Required<T> do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Required<T>`** is a built-in Utility Type that makes **all properties of type** `T` **required** (that is, it removes `?`). **Key point:** it is the opposite of `Partial<T>` - `Partial` makes all properties optional, while `Required` makes all properties required by stripping the `?` modifier from every key.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `Required<T>` does > `Required<T>` is a built-in Utility Type > that makes **all properties of type** `T` **required** (that is, it removes `?`). --- ### Example ```javascript interface User { id: number; name?: string; email?: string; } type FullUser = Required<User>; ``` Now `FullUser` looks like this: ```javascript { id: number; name: string; // no longer optional email: string; // no longer optional } ``` > All fields are now **required**, regardless of whether they were `?`. --- ## 2. How `Required<T>` is implemented Inside TypeScript it is defined roughly like this: ```javascript type Required<T> = { [P in keyof T]-?: T[P]; }; ``` Explanation: - `keyof T` walks over all keys of type `T`; - `-?` is an operator that **removes the** `?` **modifier** (makes the property required); - `T[P]` keeps the original value type. --- ## 3. How `Required<T>` is the opposite of `Partial<T>` | Utility | What it does | Example result | |---|---|---| | `Partial<T>` | Makes all properties **optional** | `{ id?: number; name?: string }` | | `Required<T>` | Makes all properties **required** | `{ id: number; name: string }` | --- ### A comparison example ```javascript interface Profile { username?: string; age?: number; } // Partial loosens the requirements: type OptionalProfile = Partial<Profile>; // { username?: string; age?: number } // Required tightens the requirements: type StrictProfile = Required<Profile>; // { username: string; age: number } ``` --- ## 4. How `Required<T>` affects optional properties It **forcibly removes the** `?` **mark** from all properties of the interface. Even if a property was optional, it becomes required when `Required` is applied. --- ### Example ```javascript interface Config { port?: number; host?: string; } type FullConfig = Required<Config>; const cfg: FullConfig = { port: 8080, host: "localhost", }; // everything must be specified ``` If you try to skip a field: ```javascript const bad: FullConfig = { port: 8080 }; // Error: property "host" is missing ``` --- ## 5. `Partial` and `Required` can be combined Sometimes you need to "bring back" the requirement for only some of the fields: ```javascript interface User { id: number; name?: string; email?: string; } // All fields optional type OptionalUser = Partial<User>; // Only name is required again type PartiallyRequired = Required<Pick<User, "name">> & Partial<User>; ``` > Now `name` is required, and the rest are optional. --- ## 6. A practical example ### Usage in form validation: ```javascript interface FormData { name?: string; email?: string; age?: number; } function validate(data: Required<FormData>) { // all fields are guaranteed to be present console.log(data.name.toUpperCase()); } validate({ name: "Tim", email: "tim@mail.com", age: 30 }); // OK validate({ name: "Tim" }); // Error - email and age are required ``` --- ## Summary | Question | Answer | |---|---| | What `Required<T>` does | Makes **all properties required**, removing `?` | | How it differs from `Partial<T>` | It is the **opposite effect** - `Partial` makes everything optional, `Required` makes everything required | | How it affects optional properties | Turns them into required properties (`?` is removed from every key) |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.