Exclude

This lesson explains the exclude mapped type.

We'll cover the following...

Exclude vs Extract

The Exclude mapped type is similar to Extract in the sense that it builds a type by selecting several properties. However, contrary to Extract, Exclude takes all the properties from a type and removes the specified one, instead of starting from nothing and adding the specified properties.

The following code snippet produces the exact same code as the one created in the previous lesson with Extract.

Press + to interact
interface Animal {
name: string;
gender: string;
sound: string;
}
interface Human {
name: string;
gender: string;
nickname: string;
}
// type LivingThing = Extract<keyof Animal, keyof Human>;
type LivingThing = Exclude<keyof Animal, "sound">;
function sayMyName(who: Record<LivingThing, string>): void {
console.log(who.name +" is of type " + who.gender);
}
const animal: Animal = { name: "Lion", sound: "Rawwwhhh", gender: "Male" };
const human: Human = { name: "Jacob", nickname: "Jaco-bee", gender: "Boy" };
sayMyName(animal);
sayMyName(human);

The ...