July 2017
Intermediate to advanced
454 pages
10h 1m
English
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.
Read now
Unlock full access