- Learn React with TypeScript 3
- Carl Rippon
- 169字
- 2021-06-10 19:16:34
Extending interfaces
Interfaces can extend other interfaces so that they inherit all the properties and methods from its parent. We do this using the extends keyword after the new interface name and before the interface name that is being extended.
Let's look at the following example:
- We create a new interface, taking Product as a base, and add information about discount codes:
interface Product {
name: string;
unitPrice: number;
}
interface DiscountCode {
code: string;
percentage: number;
}
interface ProductWithDiscountCodes extends Product {
discountCodes: DiscountCode[];
}
- We can create an instance of the interface in the usual way, filling in properties from the base interface as well as the child interface:
const table: ProductWithDiscountCodes = {
name: "Table",
unitPrice: 500,
discountCodes: [
{ code: "SUMMER10", percentage: 0.1 },
{ code: "BFRI", percentage: 0.2 }
]
};
Interfaces allow us to create complex but flexible structured types for our TypeScript program to use. They are a really important feature that we can use to create a robust, strongly-typed TypeScript program.
推薦閱讀
- Practical Data Analysis Cookbook
- 精通Nginx(第2版)
- Hyper-V 2016 Best Practices
- Cocos2d-x游戲開發:手把手教你Lua語言的編程方法
- PyQt從入門到精通
- 編寫高質量代碼:改善Python程序的91個建議
- 常用工具軟件立體化教程(微課版)
- 計算機應用基礎教程(Windows 7+Office 2010)
- Swift語言實戰晉級
- Tableau Desktop可視化高級應用
- Python機器學習開發實戰
- JavaScript Unit Testing
- 系統分析師UML用例實戰
- Build Your Own PaaS with Docker
- HTML5 and CSS3:Building Responsive Websites