Optional properties

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset