[Pick Up TS](2) 字面量对象的额外检查

295 阅读1分钟
interface LabeledValue {
  label: string;
}

function printLabel(labeledObj: LabeledValue) {
  console.log(labeledObj.label);
}

let myObj = { size: 10, label: "Size 10 Object" };

// 通过
printLabel(myObj); 

// 报错,字面量对象会有额外的类型检查
printLabel({ size: 10, label: "Size 10 Object" })