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

  • Expert Angular
  • Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
  • 83字
  • 2021-07-15 17:05:31

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.

主站蜘蛛池模板: 沁水县| 万山特区| 交口县| 崇信县| 云龙县| 瑞昌市| 清苑县| 天门市| 苗栗县| 仁化县| 蛟河市| 沾益县| 隆安县| 工布江达县| 平陆县| 夏津县| 四子王旗| 双鸭山市| 景东| 南开区| 房产| 昭觉县| 和田市| 五大连池市| 江华| 安庆市| 营山县| 阳新县| 乃东县| 亚东县| 西华县| 罗江县| 邵阳县| 芮城县| 怀柔区| 融水| 潼南县| 兰考县| 彰武县| 崇左市| 南通市|