RecipesChapter 9: The Standard Library and External Type Definitions

Iterating over Objects with Object.keys

Recipe 9.1 from The TypeScript Cookbook

function printPerson<T extends Person>(p: T) {
  const you: Person = {
    name: "Reader",
    age: NaN,
  };
  for (let k in p) {
    console.log(k, you[k]);
//                 ^
//  Type 'Extract<keyof T, string>' cannot be used to index type 'Person'
  }
}
Open in TypeScript Playground →