RecipesChapter 3: The Type System

Modeling Data with Union and Intersection Types

Recipe 3.1 from The TypeScript Cookbook

type Doll = ToyBase & {
  material: "plush" | "plastic";
};

function checkDoll(doll: Doll) {
  if (doll.material === "plush") {
    // do something with plush
  } else {
    // doll.material is "plastic", there are no other options
  }
}
Open in TypeScript Playground →