
上QQ阅读APP看书,第一时间看更新
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.