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

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.

主站蜘蛛池模板: 清镇市| 通山县| 云龙县| 饶平县| 通榆县| 南华县| 南安市| 乌拉特前旗| 江川县| 景宁| 汉沽区| 赣榆县| 邹平县| 汝州市| 定陶县| 肇州县| 塔河县| 万全县| 霍林郭勒市| 来安县| 慈溪市| 沂南县| 鄂伦春自治旗| 治多县| 崇左市| 东宁县| 中宁县| 汕尾市| 临沂市| 齐齐哈尔市| 定陶县| 武邑县| 灌阳县| 嘉兴市| 伊宁县| 泗水县| 寻甸| 乃东县| 区。| 聊城市| 成安县|