官术网_书友最值得收藏!

Optional properties using an interface

In some scenarios, we may want to pass values only for minimal parameters. In such cases, we can define the properties in an interface as optional properties, as follows:

interface Customer { 
  id: number; 
  name: string; 
  bonus?: number; 
} 
  
function addCustomer(customer: Customer) { 
  if (customer.bonus) { 
    console.log(customer.bonus); 
  } 
} 
  
addCustomer({id: 101, name: "Rajesh Gunasundaram"}); 

Here, the bonus property has been defined as an optional property by concatenating a question mark (?) at the end of the name property.

主站蜘蛛池模板: 沂南县| 原阳县| 英山县| 鹿泉市| 紫云| 林芝县| 壶关县| 平定县| 崇文区| 志丹县| 威信县| 穆棱市| 兰溪市| 兴隆县| 遵义市| 诏安县| 邢台县| 泸水县| 通河县| 深水埗区| 上饶县| 惠水县| 同江市| 稻城县| 中西区| 东阳市| 渝中区| 东阳市| 漳平市| 梓潼县| 汾西县| 渑池县| 邳州市| 若尔盖县| 墨江| 连城县| 苍溪县| 延安市| 黎平县| 都昌县| 辰溪县|