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

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.

主站蜘蛛池模板: 尚义县| 和平区| 固原市| 定边县| 嵊州市| 洪湖市| 邛崃市| 喀喇| 凤阳县| 浮梁县| 闽侯县| 和林格尔县| 镇宁| 布尔津县| 鹤庆县| 崇左市| 仙居县| 中超| 阳山县| 平泉县| 绵阳市| 军事| 将乐县| 宾阳县| 库尔勒市| 凤山市| 临朐县| 临沭县| 江津市| 武邑县| 惠安县| 庐江县| 贵阳市| 东海县| 屏山县| 泉州市| 高州市| 会理县| 喀喇沁旗| 灌阳县| 福安市|