Difference b/w only Exclude and Omit (Pick & Exclude) Typescript

You are right about Pick, it takes an object type and extracts the specified properties. So:

 Pick<{ a: string, b:string }, 'a' > === { a: string } 

The opposite of this is actually the later added Omit. This type takes an object type and removes the specified properties from the type.

 Omit<{ a: string, b:string }, 'a' > === { b: string }

Exclude is a different beast, it takes a union type and removes a constituent of that union.

Exclude<string | number, string > === number

Exclude is defined as:

type Exclude<T, U> = T extends U ? never : T;

This means that Exclude will return never if T extends U, and T if it does not. So this means that:

  • Exclude<string, number> is string
  • Exclude<string, string> is never

The thing is that conditional types distribute over naked type parameters. So this means that if applied to a union we get the following:

Exclude<string | number, number>
    => Exclude<string, number> | Exclude<number, number> // Exclude distributes over string | number
    => string | never => // each application of Exclude resolved to either T or never
    => string  // never in unions melts away

Exclude is used in the definition of Omit. Exclude<keyof T, K> takes a union of the keys of T and removes the keys specified by K. And then Pick extracts the remaining properties from T.

Edit

While both Omit and Exclude take two type arguments (and no relationship is enforced between the two) they can not be use interchangeably. Look at the result of some applications of these types:

type T0 = Omit<{ a: string, b: string }, "a"> //  { b: string; }, a is removed 
type T1 = Exclude<{ a: string, b: string }, "a"> // { a: string, b: string }, a does not extend { a: string, b: string } so Exclude does nothing 

type T2 = Omit<string | number, string> // Attempts to remove all string keys (basically all keys) from string | number , we get {}
type T3 = Exclude<string | number, string> // string extends string so is removed from the union so we get number

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)