Suggest an editImprove this articleRefine the answer for “What does Omit<T, K> do?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Omit<T, K>`** creates a new type based on `T`, **excluding** the properties listed in `K`. **Key point:** `Pick` "takes what's needed" while `Omit` "removes what's not" - they are opposite in purpose.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `Omit<T, K>` does > `Omit<T, K>` creates a new type based on `T`, > **excluding** the properties listed in `K`. --- ### Syntax ```javascript Omit<SourceType, 'key1' | 'key2'> ``` --- ### Example ```javascript interface User { id: number; name: string; email: string; password: string; } type PublicUser = Omit<User, 'password'>; ``` Now `PublicUser` is equivalent to: ```javascript { id: number; name: string; email: string; } ``` > The `password` field is excluded from the type. --- ## 2. How `Omit` is the opposite of `Pick` | Utility | What it does | Example | |---|---|---| | `Pick<T, K>` | **keeps only the specified** properties | `Pick<User, 'id' \| 'name'>` | | `Omit<T, K>` | **removes the specified** properties | `Omit<User, 'password'>` | Visually: ```javascript interface User { id: number; name: string; email: string; password: string; } // Pick type MinimalUser = Pick<User, 'id' | 'name'>; // { id: number; name: string } // Omit type SafeUser = Omit<User, 'password'>; // { id: number; name: string; email: string } ``` > That is: > **Pick = "take what's needed"**, > **Omit = "remove what's not"**. --- ## 3. How `Omit<T, K>` works internally The language-level implementation of `Omit` looks like this: ```javascript type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>; ``` What happens: 1. `keyof T` takes all the keys of the source type. 2. `Exclude<keyof T, K>` removes from them the ones listed in `K`. 3. `Pick<...>` creates a new type based on the remaining properties. --- ## 4. Can you remove several properties with `Omit` Yes, you can specify several properties at once via a union (`|`): ```javascript type UserWithoutSensitive = Omit<User, 'password' | 'email'>; ``` The result is: ```javascript { id: number; name: string; } ``` > TypeScript removes **all the listed fields** from the resulting type. --- ## 5. Usage example in real projects ### Removing sensitive data ```javascript interface Employee { id: number; name: string; email: string; salary: number; } type PublicEmployee = Omit<Employee, 'salary'>; ``` > This type is convenient for safely returning data from an API. --- ### Simplified forms ```javascript interface FormData { id: number; name: string; createdAt: Date; updatedAt: Date; } type FormInput = Omit<FormData, 'id' | 'createdAt' | 'updatedAt'>; ``` > The form only needs the input fields (`name`), > while the rest are generated automatically. --- ## 6. An error for a nonexistent key If you try to remove a field that does not exist on the type, TypeScript immediately shows an error: ```javascript type Wrong = Omit<User, 'nickname'>; // Error: Type '"nickname"' is not assignable to keyof 'User' ``` > That is, `Omit` checks that the specified keys actually exist on `T`. --- ## Summary | Question | Answer | |---|---| | What does `Omit<T, K>` do | Creates a new type without the specified properties | | How it differs from `Pick` | `Pick` selects the needed fields, `Omit` removes the unwanted ones | | Can you remove several properties | Yes, via `'prop1' \| 'prop2'` | | What happens if you remove a nonexistent one | Error: key not found on the type |For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.