RecipesChapter 4: Generics

Modifying Objects with Assertion Signatures

Recipe 4.7 from The TypeScript Cookbook

function check<T>(obj: T): asserts obj is T & { checked: true } {
  (obj as T & { checked: boolean }).checked = true;
}

const person = {
  name: "Stefan",
  age: 27,
};

check(person);
Open in TypeScript Playground →