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

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.

主站蜘蛛池模板: 大兴区| 新巴尔虎右旗| 荆门市| 资源县| 栾川县| 澎湖县| 田林县| 无为县| 敦煌市| 颍上县| 延川县| 平度市| 海淀区| 庆元县| 千阳县| 新民市| 修水县| 大新县| 扶风县| 襄汾县| 黄山市| 阜新市| 海城市| 江华| 凤城市| 巴彦县| 离岛区| 新龙县| 靖远县| 固阳县| 和林格尔县| 巩义市| 大丰市| 多伦县| 常山县| 介休市| 镇巴县| 华安县| 游戏| 西和县| 那坡县|