TypeScript Reference ยท Utility Types
TypeScript Omit<T, K> utility type
Reference for TypeScript Omit<T, K>: remove properties from an object type, why K is not checked against keyof T, and how it behaves with unions.
Omit<T, K> builds a type with every property of T except those named in K. Optional and readonly modifiers on the remaining properties are preserved. It is the natural way to describe "this type, minus a few fields": a creation payload without the generated id, or a public record without secrets.
Unlike Pick<T, K>, Omit does not check that K names real properties of T. That is a deliberate trade-off, and the main source of mistakes.
interface Post { id: number; title: string; body: string; } type NewPost = Omit<Post, "id">; // { title: string; body: string }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
| Name | Description |
|---|---|
T |
The source object type. |
K |
A key or union of keys to remove. Any string, number or symbol is accepted, even if T has no such key. |
Result: a mapped type with every key of T that is not in K.
Edge cases: Omit<T, never> is a copy of T. If K is string (not a literal), every string key is removed, which usually empties the type.
Usage notes
- No key checking.
Omit<Post, "titel">compiles and removes nothing. Define aStrictOmitwithK extends keyof T(as above) when you want errors on typos. - Does not distribute over unions.
Omit<A | B, "x">first computeskeyof (A | B), which is only the shared keys, and returns a single object type. To omit from each member, use a distributive helper:T extends unknown ? Omit<T, K> : never. - Loses index signatures. For a type with
[key: string]: unknown,keyof Tisstring | number, so omitting one named key removes everything. AvoidOmiton such types. - Compared with
Pick: useOmitwhen you remove few keys,Pickwhen you keep few. - Interfaces: you can extend the result:
interface Draft extends Omit<Post, "id"> { draftId: string }. - Runtime: type-level only. Build the new object (destructuring with rest:
const { id, ...rest } = post) to drop data. - Added in TypeScript 3.5.