Built-in Conditional Types

This lesson walks through several of TypeScript's built-in conditional types and explains them in detail.

We'll cover the following

Exclude

Exclude takes a union type, T, and removes the members that are assignable to U. It’s another example of the distributiveness of conditional types. For all union members that extend U, the conditional expression will return never. The result will be a union of the remaining members.

Exclude is particularly useful when combined with the keyof operator. If you’re not familiar with it, keyof takes an interface and returns a union type of literal types representing names of properties of this interface.

As you can see in Example 3, Exclude can be used to allow passing all object properties to a function except some specific property (or properties). In this example, safeSetProp is a generic function with two type parameters, T and K. We require that K extends Exclude<keyof T, 'id'> as it must be a literal type representing one of the properties except for 'id'.

Get hands-on with 1200+ tech skills courses.