- Expert Angular
- Mathieu Nayrolles Rajesh Gunasundaram Sridhar Rao
- 83字
- 2021-07-15 17:05:31
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.
推薦閱讀
- 零基礎學C++程序設計
- GraphQL學習指南
- Java Web基礎與實例教程(第2版·微課版)
- 編寫高質量代碼:改善Python程序的91個建議
- Learning Unity 2D Game Development by Example
- 執劍而舞:用代碼創作藝術
- CoffeeScript Application Development Cookbook
- Java零基礎實戰
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Android編程權威指南(第4版)
- Java高級程序設計
- TypeScript全棧開發
- Design Patterns and Best Practices in Java
- Mastering R for Quantitative Finance
- TensorFlow+Keras深度學習算法原理與編程實戰